# 新手，求问鸡，兔问题

qq_39156427 2017-11-04 11:39:59

#include <stdio.h>
int main()
{
int n,m,a=0,i=0;
scanf("%d %d",&n,&m);
if(m>=2*n&&m<=4*n&&m%2==0&&m!=0&&n!=0)
{{while(m!=4*n-2*a)
a++;}
int b=n-a;
printf("%d %d",a,b);}
else
{printf("Error");}
}
...全文
140 4 点赞 打赏 收藏 举报

4 条回复

``````#include<stdio.h>
int main()
{
int n[50],m[50],i=0,j,k; //j，k为鸡和兔
while(2 == scanf("%d %d",&n[i],&m[i]) && i < 50)
{
if(n[i] == 0|| m[i] == 0)
break;
i++;
}

i = 0;
while (n[i] != 0 || m[i] != 0) {
for (j = 0; j < n[i]; j++) {
k = n[i]-j;
if (n[i] == j + k && m[i] == 2*j+4*k)
break;
}
if(n[i] == j + k && m[i] == 2*j+4*k)
printf("%d %d\n",j, k);
else
printf("Error\n");
i++;
}
return 0;
}``````

• 打赏
• 举报

``````#include <stdio.h>

int main()
{
int chicken,rabbit;//鸡，兔子
int M,N;//头，脚
printf("M=,N=");
scanf("%d %d",&M,&N);
for(chicken=0;chicken<=M;chicken++)
{
if(chicken*2+rabbit*4==N)//如果总脚数与输入的总脚数相同
{
printf("%d %d",chicken,rabbit);//分别输出鸡、兔
return 0;
}
}
//for执行完表示找不到
printf("0");
return 0;
}``````

• 打赏
• 举报

flying_music 2017-11-05
``````
#include <stdio.h>

int main()
{
int n[20] = {0},m[20] = {0}, a=0, i=0;
int sum = 0;
int b = 0;
printf("请输入组数：\n");
scanf("%d", &sum);
for(i=0; i<sum; i++){
printf("第%d组：\n", i+1);
scanf("%d %d",n+i, m+i);
}
printf("\n结果:\n");
for(i=0; i<sum; i++){
a = 0;
printf("第%d组：\n", i+1);
if(m[i]>=2*n[i] && m[i]<=4*n[i] && m[i]%2==0 && m[i]!=0 && n[i]!=0)
{
while(m[i] != 4*n[i]-2*a)
a++;
b=n[i]-a;
printf("%d %d\n",a,b);
}
else{
printf("Error");
}
}
return 0;
}
``````
• 打赏
• 举报

• 打赏
• 举报

3.3w+

C/C++ 新手乐园

2017-11-04 11:39