應用Java代碼停止因數分化和求最小公倍數的示例。本站提示廣大學習愛好者:(應用Java代碼停止因數分化和求最小公倍數的示例)文章只能為提供參考,不一定能成為您想要的結果。以下是應用Java代碼停止因數分化和求最小公倍數的示例正文
因數分化
/*
因數分化是非常根本的數學運算,運用普遍。上面的法式對整數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));
}
}
運轉成果:
24