目录
1.1 百钱百鸡问题
代码如下:
#include<stdio.h>
int main(){
int a, b, c;
for (a = 0; a <=20; a++){
for (b = 0; b <=33; b++){
for (c = 0; c <=100; c++){
if (a + b + c == 100 && 5 * a + 3 * b + c/3 == 100){
printf("公鸡有%d,母鸡有%d,小鸡有%d\n", a, b, c);
}
}
}
}
return 0;
}
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/d581c642d7d29917b46d679ec7d04ba5.png)
代码优化:
#include<stdio.h>
int main(){
int a, b, c;
for (a = 0; a <=20; a++){
for (b = 0; b <=33; b++){
//for (c = 0; c <=100; c++){
c = 100 - a - b;
if ( /*a + b + c == 100 &&*/ 5 * a + 3 * b + c/3 == 100){
printf("公鸡有%d,母鸡有%d,小鸡有%d\n", a, b, c);
//}
}
}
}
return 0;
}