#include <stdio.h>
void main( )
{
int ret = 0; // 表示最后一个printf的返回值
while(1)
{
int h,f,c,r;
printf("请输入头的数目:");
scanf("%d",&h);
printf("\n请输入脚的数目(必须为偶数):");
scanf("%d",&f);
while(f%2!=0 && f != 0 && h != 0) //这里的不等于的条件可以不加
{
printf("\n输入脚的数目为奇数,请重新输入脚的数目(必须偶数):");
scanf("%d",&f);
if( f > 4h || f < 2h)
continue;
}
for(c=0;c<=h;c++)
for(r=1;r<=h;r++) //这句改成:for(r=0; r <= h-c; r++)好点,减少的循环的次数,加了效率
{
if(c+r==h&&2*c+4*r==f)
ret = printf("\n鸡的数目为%d,兔子的数目为%d\n\n",c,r);
}
if( ret == 39) /*作用是输入一次成功后,结束大循环, 可以不要*/
break;
}
}
温馨提示:答案为网友推荐,仅供参考