因數分解
/* 因數分解是十分基本的數學運算,應用廣泛。下面的程序對整數n(n>1)進行因數分解。 比如,n=60, 則輸出:2 2 3 5。請補充缺失的部分。 */ public class 因數分解 { public static void f(int n) { for (int i = 2; i < n / 2; i++) { while(n%i==0){ // 填空 System.out.printf("%d ", i); n = n / i; } } if (n > 1) System.out.printf("%d/n", n); } public static void main(String[] args) { f(60); } }
運行結果:
2 2 3 5
最小公倍數
/* 求兩個數字的最小公倍數是很常見的運算。比如,3和5的最小公倍是15。6和8的最小公倍數是24。 下面的代碼對給定的兩個正整數求它的最小公倍數。請填寫缺少的代碼,使程序盡量高效地運行。 把填空的答案(僅填空處的答案,不包括題面)存入考生文件夾下對應題號的“解答.txt”中即可。 */ public class 最小公倍數 { public static int f(int a, int b) { int i; for(i=a;;i+=a){ // 填空 if(i%b==0) return i; } } public static void main(String[] args){ System.out.println(f(6,8)); } }
運行結果:
新聞熱點
疑難解答