我是matlab初学者 这个程序看不懂。。。第一个break有什么用啊第二个break不是一直都在运行吗?
鸡兔同笼问题啦i=1;whilei>0ifrem(100-i*2,4)==0&(i+(100-i*2)/4)==36break;endi=i+1;n1=i;n2=(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); 展开
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); 展开
1个回答
展开全部
你的程序有问题,其有效成分仅仅如下:
i=1;
i=i+1;
n1=i;
n2=(100-2*i)/4;
fprintf('the number of chicken is %d.\n',n1);
fprintf('the number of rabbit is %d.\n',n2);
从上面看你并没有解决鸡兔同笼问题。
以下是按照你的程序修改的
clear;
clc;
i=1;
while i>0
if rem(100-i*2,4)==0&(i+(100-i*2)/4)==36
n1 = i;
n2 = (100-2*i)/4;
break;
end
i=i+1;
end
fprintf('the number of chicken is %d.\n',n1);
fprintf('the number of rabbit is %d.\n',n2);
结果:
the number of chicken is 22.
the number of rabbit is 14.
当然也可以不用这么复杂,可以采用以下程序:
[n1,n2] = solve('2*x+4*y-100','x+y-36'); %%%求解个方程组就成
fprintf('the number of chicken is %d.\n',double(n1));
fprintf('the number of rabbit is %d.\n',double(n2));
i=1;
i=i+1;
n1=i;
n2=(100-2*i)/4;
fprintf('the number of chicken is %d.\n',n1);
fprintf('the number of rabbit is %d.\n',n2);
从上面看你并没有解决鸡兔同笼问题。
以下是按照你的程序修改的
clear;
clc;
i=1;
while i>0
if rem(100-i*2,4)==0&(i+(100-i*2)/4)==36
n1 = i;
n2 = (100-2*i)/4;
break;
end
i=i+1;
end
fprintf('the number of chicken is %d.\n',n1);
fprintf('the number of rabbit is %d.\n',n2);
结果:
the number of chicken is 22.
the number of rabbit is 14.
当然也可以不用这么复杂,可以采用以下程序:
[n1,n2] = solve('2*x+4*y-100','x+y-36'); %%%求解个方程组就成
fprintf('the number of chicken is %d.\n',double(n1));
fprintf('the number of rabbit is %d.\n',double(n2));
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询