基本上就是这样了,是跟着老师打出来的雏形,因为上课恍惚了,就没听仔细,希望有高手能指点指点,此程序哪里还需要完善,谢谢,如果有兴趣交流的请加我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;
}
}
----------------解决方案--------------------------------------------------------
太夸张了吧,求两个数的最小公倍数至于写这么多代码吗????????
----------------解决方案--------------------------------------------------------
看看这个怎么样:
import java.io.*;
public class yueshu
{
public static void main(String args[])throws IOException
{ String s1=new String();
String s2=new String();
int n1,n2,r,p;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("insert two number:");
s1=br.readLine();
s2=br.readLine();
n1=Integer.parseInt(s1);
n2=Integer.parseInt(s2);
if(n1<n2)
{ int t=n1;
n1=n2;
n2=t;
}
p=n1*n2;
while(n2!=0)
{ r=n1%n2;
n1=n2;
n2=r;
}
System.out.println("最大公约数是:"+n1);
System.out.ptintln("最小公倍数是:"+p/n1);
}
}
----------------解决方案--------------------------------------------------------
不错,简单明了
----------------解决方案--------------------------------------------------------
记得有个递归的算法,程序简短至极
----------------解决方案--------------------------------------------------------
我也写一个。。希望大家指正。。
import java.io.*;
import java.util.*;
public class two
{
public static void main(String args[])
throws IOException
{
System.out.println("请输入要求最小公倍数的两个数,中间用空格隔开:");
BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
String s=a.readLine();
StringTokenizer b=new StringTokenizer(s," ");
int e[]=new int[2];
String c=b.nextToken();
e[0]=Integer.parseInt(c);
String d=b.nextToken();
e[1]=Integer.parseInt(d);
int sum=1;
if((e[0]!=0)&&(e[1]!=0)){for(;;sum++){if((sum%e[0])+(sum%e[1])==0)break;}System.out.println("所求的最小公倍数
是:"+sum);}else System.out.println("输入的两个数中有一个是0,非法!");
}
}
----------------解决方案--------------------------------------------------------