题目:
HJ72 百钱买百鸡问题
题解:
暴力枚举。
- 鸡翁一值钱五,鸡翁最多买20只
- 鸡母一值钱三,鸡母最多买100/3只
- 鸡雏三值钱一,鸡雏最多买100只
private void buyChicken() {
int totalCount = 100;
int totalMoney = 100;
for (int i = 0; i <= 20; i++)
for (int j = 0; j<= 34; j++)
for (int k = 0; k <= 100; k=k+3) {
int count = i + j + k;
int money = i * 5 + j * 3 + k / 3;
if (count == totalCount && money == totalMoney) {
System.out.printf("%s %s %s\n", i, j, k);
}
}
}
鸡雏三值钱一,所以鸡雏的数量一定是3的倍数,这样才能凑成整数。
时间复杂度:O()