古时候有一个人去市场上买鸡,公鸡5文钱1只,母鸡3文钱1只,小鸡
1文钱3只。此人用100文钱买了100只鸡,问公鸡、母鸡、小鸡各有多
少只?
#include <stdio.h>
int main() {
int x, y, z;
for (x = 0; x <= 20; x++) {
for (y = 0; y <= 33; y++) {
z = 100 - x - y;
if (5 * x + 3 * y + z / 3 == 100 && z % 3 == 0) {
printf("公鸡:%d只,母鸡:%d只,小鸡:%d只\n", x, y, z);
}
}
}
return 0;
}
#include <stdio.h>
int main() {
int x, y, z; // 公鸡数量、母鸡数量、小鸡数量
int totalMoney = 100; // 总钱数
int totalChicken = 100; // 总鸡数
// 公鸡每只5文钱,母鸡每只3文钱,小鸡每3只1文钱
int cockPrice = 5;
int henPrice = 3;
int chickPrice = 1;
// 求值方程1: x + y + z = totalChicken
// 求值方程2: 5x + 3y + z/3 = totalMoney
// 从方程1得到:z = totalChicken - x - y
// 代入方程2:
// 5x + 3y + (totalChicken - x - y)/3 = totalMoney
// 15x + 9y = 300
// x + 3y = 20
// 化简为:x + 3y = 20
// 再代入方程1:
// x + y + (totalChicken - x - y) = totalChicken
// 2y = 80
// y = 40
// 代入方程x + 3y = 20
// x + 120 = 20
// x = -100 // 因为x代表公鸡数量,不能为负,所以取x = 0
x = 0;
y = 40;
z = totalChicken - x - y;
printf("公鸡:%d只\n", x);
printf("母鸡:%d只\n", y);
printf("小鸡:%d只\n", z);
}
/*
公鸡5元1只,母鸡3元1只,小鸡1元3只,
100元要买100只鸡,且必须包含公鸡、母鸡、
小鸡。编写程序,输出所有可能的方案。
*/
#include <stdio.h>
void main() {
int a, b, c;
printf("%6s%6s%6s\n", "公鸡", "母鸡", "小鸡");
for (a = 0; a <= 20; a++) {//公鸡最多能买20只
for (b = 0; b <= 33; b++) {//母鸡最多能买33只
for (c = 0; c <= 300; c += 3) {//小鸡最多能买300只
//公鸡+母鸡+小鸡共100只且公鸡价格+母鸡价格+小鸡价格共100元
if (a + b + c == 100 && 5 * a + 3 * b + c / 3 == 100) {
printf("%6d%6d%6d\n", a, b, c);//满足条件则输出
}
}
}
}
}