| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2182 人关注过本帖
标题:[求助]求两个数的最小公倍数
只看楼主 加入收藏
最多才七个字啊
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2006-4-17
收藏
 问题点数:0 回复次数:5 
[求助]求两个数的最小公倍数

基本上就是这样了,是跟着老师打出来的雏形,因为上课恍惚了,就没听仔细,希望有高手能指点指点,此程序哪里还需要完善,谢谢,如果有兴趣交流的请加我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;
}



}

搜索更多相关主题的帖子: 最小公倍数 
2006-05-07 16:18
无铭剑客
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2005-10-31
收藏
得分:0 

太夸张了吧,求两个数的最小公倍数至于写这么多代码吗????????

2006-05-09 18:45
oujianqiang
Rank: 1
等 级:新手上路
帖 子:138
专家分:0
注 册:2006-5-16
收藏
得分:0 
看看这个怎么样:
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);
}
}

2006-05-18 16:30
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
不错,简单明了

可惜不是你,陪我到最后
2006-05-18 16:33
ycxct
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-4-17
收藏
得分:0 
记得有个递归的算法,程序简短至极
2006-05-18 23:23
cumtkl
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2006-5-10
收藏
得分:0 

我也写一个。。希望大家指正。。
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,非法!");
}
}

2006-05-19 17:45
快速回复:[求助]求两个数的最小公倍数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022517 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved