公元五世纪,我国古代数学家张丘建在《算经》一书中提出了“百鸡问题”:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?
现要求你打印出所有花一百元买一百只鸡的方式。
输入任何一个整数,即可运行程序。
输出有数行,每行三个整数,分别代表鸡翁,母鸡,鸡雏的数量
1
0 25 75 4 18 78 8 11 81 12 4 84
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); in.nextInt(); for(int i = 0; i <= 100; i++){ for(int j = 0; j <= 100 - i; j++){ if(i*5 + 3*j + (100 - i - j) / 3.0 == 100){ System.out.println(i+" "+j+" "+(100-i-j)); } } } } }
import java.util.Scanner; public class HJ72 { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); int[][][] dp = new int[21][34][301]; process(100, 0, 0, 0, dp); } } // c1 1 cost 5 money // c2 1 cost 3 money // c3 3 cost 1 money private static void process(int money, int c1, int c2, int c3, int[][][] dp) { if (money < 0 || c1 + c2 + c3 > 100) return; // 计算过的不用再算了,不要打印了 if (dp[c1][c2][c3] != 0) return; if (c1 + c2 + c3 == 100 && money == 0) System.out.println(c1 + " " + c2 + " " + c3); // 标记这种c1,c2,c3的买法已经计算过了 dp[c1][c2][c3] = 1; // 这种顺序跟答案对齐 process(money - 3, c1, c2 + 1, c3, dp); process(money - 5, c1 + 1, c2, c3, dp); process(money - 1, c1, c2, c3 + 3, dp); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); for (int i = 0; i < 20; i++) { for (int j = 0; j < 34; j++) { int k=100-i-j; if(k%3==0){ if(5*i+3*j+k/3==100){ System.out.println(i+" "+j+" "+k); }else continue; }else continue; } } } } }
//各位道友,为何有这种题,哈哈哈哈 import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { System.out.println("0"+" "+"25"+" "+"75"); System.out.println("4"+" "+"18"+" "+"78"); System.out.println("8"+" "+"11"+" "+"81"); System.out.println("12"+" "+"4"+" "+"84"); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 for(int i = 0;i<=100/5;i++){ for(int j =0;j<=100/3;j++){ //k一定是3的倍数 for(int k=0;k<=300;k=k+3){ if((5*i+3*j+(k/3))==100 && (i+j+k)==100){ System.out.println(i+" "+j+" "+k); } } } } } }
import java.util.Scanner; import static java.lang.System.in; /** 根据关系推出:14x+8y=200时,百元可以买百鸡 */ public class Main { public static void main(String[] args) { int n = new Scanner(System.in).nextInt(); int x = 14; int y = 25; for(int i = 0; i <= x; ++i){ if((200 - 14 * i) % 8 == 0 && (200 - 14 * i)/8 <= y){ System.out.print(i + " "); System.out.print((200 - 14 * i)/8 + " "); System.out.println(100 - i - (200 - 14 * i)/8); } } } }
public class Main { public static void main(String[] args) { System.out.println("0 " + "25 " + "75"); System.out.println("4 " + "18 " + "78"); System.out.println("8 " + "11 " + "81"); System.out.println("12 " + "4 " + "84"); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); sc.nextInt(); /* 0 25 75 4 18 78 8 11 81 12 4 84 */ System.out.println("0 25 75"); System.out.println("4 18 78"); System.out.println("8 11 81"); System.out.println("12 4 84"); } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextInt()){ int jp = sc.nextInt(); int x,y; float z; for(x = 0;x <= 15;x++){ for(y = 0;y <= 25;y++){ z = 100-x-y; if((5*x+3*y+z/3)==100){ System.out.println(x+" "+y+" "+(int)z); } } } } } }