首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

鸡兔同笼的matlab解决,出有关问题了

2013-03-22 
鸡兔同笼的matlab解决,出问题了%该程序用于求解经典的鸡兔同笼问题%鸡兔同笼,头36,脚100%求鸡兔个多少只i

鸡兔同笼的matlab解决,出问题了
%该程序用于求解经典的鸡兔同笼问题
%鸡兔同笼,头36,脚100
%求鸡兔个多少只
i=1;
while i>0
    if rem(100-i*2,4)==0&(i+(100-i*2)/4)==36
        break;
    end
    i=i+1;
    n1=i;
    n2=(100-2*i)/4;
    break
end
fprintf('The number of chicken is %d.\n',n1);
fprintf('The number of rabbit is %d.\n',n2);
各位大神帮忙看看吧,怎么算出来不对啊,程序没有问题啊? matlab
[解决办法]
比着LZ的想法,写了个C的,LZ的逻辑,惨不忍睹,不管C也好,matlab也好,正经看书。


# include <stdio.h>

int main()
{
    int i = 0;
    while (i <= 36)
    {
        if (((100 - i * 2) % 4 == 0) && ((i + (100 - i * 2) / 4) == 36))
        {
            printf("# chicken: %d\n", i);
            printf("# rabbit: %d\n", (100 - 2 * i) / 4);
        }
        i++;
    }

    return 0;
}


[解决办法]
i=1;
 while i>0
     if rem(100-i*2,4)==0&(i+(100-i*2)/4)==36
         break;
     end
     i=i+1;
     n1=i;
     n2=(100-2*i)/4;
    % break    %这个break多余
 end
 fprintf('The number of chicken is %d.\n',n1);
 fprintf('The number of rabbit is %d.\n',n2);

热点排行