题解 | #百钱买百鸡问题#
百钱买百鸡问题
http://www.nowcoder.com/practice/74c493f094304ea2bda37d0dc40dc85b
import java.util.Scanner;
/**
*
* 【百钱买百鸡】
*
* 公元五世纪,我国古代数学家张丘建在《算经》一书中提出了“百鸡问题”:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?
* 现要求你打印出所有花一百元买一百只鸡的方式。
*
* 【鸡雏的数量必须是3的倍数】其实让鸡雏从99往下遍历更快,但是结果与标准答案顺序不一致,无法通过案例
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int nextInt = sc.nextInt();
for (int i = 0; i <= 99; i = i + 3) {
int leftCount = 100 - i;
int leftMoney = 100 - i / 3;
for (int j = 0; j < leftCount; j++) {
int fatherCount = j;
int motherCount = leftCount - j;
if (5 * fatherCount + 3 * motherCount == leftMoney) {
System.out.println(fatherCount + " " + motherCount + " " + i);
}
}
}
}
}
小天才公司福利 1214人发布