题目:
编写一个程序解鸡兔同笼问题:已知鸡兔总数为a, 鸡兔腿总数为b, 计算出鸡兔各多少只
题目分析:直接使用顺序结构就行
1.设鸡有x只,兔子有y只,因此x+y=a;即, x=a-y
2.由于鸡有两只腿,兔子有4只腿,因此2x+4y=b;即,y=(b-2x)/4;
#include <stdio.h>
int main()
{
int a, b,x=1,y=1;
printf("Please enter the total number of chickens and rabbits:\n");
scanf("%d", &a);
printf("Please enter the total number of chicken and rabbit legs:\n");
scanf("%d", &b);
x = a-y;
y = (b-2*x)/4;
if (b % 2 == 1 || x < 0 || y < 0)
printf("false\n");
else
{
printf("How many chickens there are:%d\n", x);
printf("How many rabits there are:%d\n", y);
}
return 0;
}