百千白鸡问题
1 package com.lovo; 2 3 public class test12 { 4 public static void main(String[] args) { 5 for (int i = 0; i <= 20; i++) { 6 for (int j = 0; j <= 33; j++) { 7 for (int k = 0; k <= 100; k++) { 8 if ((5 * i + 3 * j + k / 3 == 100) && (i + j + k == 100)&&k%3==0) { 9 System.out.printf("公鸡%d只\n母鸡%d只\n小鸡%d只\n\n", i, j, k); 10 } 11 } 12 } 13 } 14 } 15 16 }
1到100内素数
package com.lovo; public class test14 { public static void main(String[] args) { System.out.println("1到100内素数有:"); for(int i=2;i<=100;i++){ int sum=0; for(int j=1;j<=i;j++){ if(i%j==0){ sum++; } } if(sum==2){ System.out.print(i+" "); } } } }
两个数的最大公约数和最小公倍数
package com.lovo; import java.util.Scanner; public class test13 { public static void main(String[] args) { int s=0; Scanner sc=new Scanner(System.in); System.out.println("n="); int n=sc.nextInt(); System.out.println("m="); int m=sc.nextInt(); for(int i=Math.min(m, n);i>=1;i--){ if((n%i==0)&&(m%i==0)){ System.out.println(n+"和"+m+"最大公约数为:"+i); break; } } for(int j=Math.max(n, m);j<=n*m;j++){ if((j%n==0)&&(j%m==0)){ System.out.println(n+"和"+m+"最小公倍数为:"+j); break; } } sc.close(); } }
时间: 2024-12-17 09:18:34