基本上就是这样了,是跟着老师打出来的雏形,因为上课恍惚了,就没听仔细,希望有高手能指点指点,此程序哪里还需要完善,谢谢,如果有兴趣交流的请加我QQ:329392019
求两个数的最小公倍数
package lab03;
import javax.swing.JOptionPane;
public class lab03 {
public static void main(String[]args){
int num1=Integer.parseInt( JOptionPane.showInputDialog(null,"第一个整数"));
int num2=Integer.parseInt( JOptionPane.showInputDialog(null,"第二个整数"));
int result=lcm(num1,num2);
JOptionPane.showMessageDialog(null,num1+"和"+num2+"的最小共被数"+result);
}
public static int lcm(int num1,int num2){
int [][]factors1=findFactors(num1);
int [][]factors2=findFactors(num2);
int [][]combineFactors=combine(factors1,factors2);
Object computedFactors;
return compute(computedFactors);
}
private static int compute(Object computedFactors) {
int computedFactors1=1;
Object combineFactors;
for(i=1;i<=combineFactors.length;i++)
computedFactors1*=combineFactors[i][2];
}
private static int[][] combine(int[][] factors1, int[][] factors2) {
int [][] result=new int[factors1.length +factors2.length ][2];
int index=0,index1=0,index2=0;
boolean end1=false,end2=false;
while(!end1||!end2){
if(factors1[index1][0]>factors2[index2][0]){
result[index][0]=factors2[index2][0];
result[index][1]=factors2[index2][1];
index2++;
}
else if(factors1[index1[0]<factors2[index2][0]){
result[index][0]=factors1[index1][0];
result[index][1]=factors1[index1][1];
index1++;
}
else{
result[index][0]=factors1[index1][0];
result[index][1]=Math.max(factors1[index1][1],factors2[index2][1]);
index1++;
index2++;
}
index++;
end1=ture;
end2=ture;
}
return result[index][2];
}
public static int[][] findFactors(int num){
int [][]factors=new int[num/2][2];
int index=0;
for(int i=2;i<=num;i++){
if (num%i==0){
while(num%i==0){
factors[index][1]++;
num/=i;
}
factors[index][0]=i;
index++;
}
}
return factors;
}
}