用Java求两个数的最大公约数和最小公倍数

 我来答
Wangx_20
2013-09-16 · TA获得超过388个赞
知道答主
回答量:25
采纳率:0%
帮助的人:15.6万
展开全部
//最大公约数
public static int getGreatestCommonDivisor(int x,int y){
int max,min,r;
if(x<y){
max=y;min=x;
}else{
max=x;min=y;
}
while((r=max%min)!=0){
max=min;
min=r;
}
return min;
}
//最小公倍数
public static int getLeastCommonMultiple(int x,int y){
return (x*y)/getGreatestCommonDivisor(x,y);
}
追问
太给力了,你的回答完美解决了我的问题!
001*********63
2013-09-16 · 超过21用户采纳过TA的回答
知道答主
回答量:87
采纳率:0%
帮助的人:22.5万
展开全部
最大公约数用辗转相除发可以得出。
最小公倍数等于两个数的乘积除以最大公约数
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
我的资讯圈
2013-09-16
知道答主
回答量:35
采纳率:0%
帮助的人:18.5万
展开全部
最小公倍数一会儿发上来,这是最大公约数

import java.util.Scanner;
public class sbAppend{
public static void main(String[] args) {
int one = 0;
int two = 0;
int n = 1;
int max = 0;

Scanner input = new Scanner(System.in);
System.out.println("请输入第一个数:");
one = input.nextInt();
System.out.println("请输入第二个数:");
two = input.nextInt();

while (n <= one) {
//此时n为公约数
if (one % n == 0 && two % n == 0) {
if (n>max) {
max = n;
}
n++;
continue;
}
n++;
}
System.out.println("最大公约数为:"+max);
}
}
这个是求最小公倍数的,希望采纳,谢谢
import java.util.Scanner;
public class sbAppend{
public static void main(String[] args) {
int one = 0;
int two = 0;
int n = 1;
int min = 10000000;
boolean flag = true;

Scanner input = new Scanner(System.in);
System.out.println("请输入第一个数:");
one = input.nextInt();
System.out.println("请输入第二个数:");
two = input.nextInt();

while (flag) {
//此时n为公倍数数
if (n % one == 0 && n % two == 0) {
if (n < min) {
min = n;
break;
}
}
n++;
}
System.out.println("最小公倍数为:"+min);
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
qbj2002
2013-09-16 · TA获得超过514个赞
知道小有建树答主
回答量:553
采纳率:0%
帮助的人:300万
展开全部
function commonDivisor(x,y){
if(isNaN(x) || isNaN(y)) return "非法输入数据";
var result =[];
var max = Math.max(x,y);
var temp = 1;
while(temp<=max){
if(x%temp==0 && y%temp==0){
result.push(temp);
}else{

}
temp++;
}
return result;
}
//求两个数的最小公倍数
function commonMultiple(x,y){
if(isNaN(x) || isNaN(y)) return "非法输入数据";
var amass = x*y;
var min = Math.min(x,y);
var temp = amass;
var result=1;
while(temp>=min && temp<=amass){
if(temp%x==0 && temp%y==0){
result = temp;
}else{

}
temp--;
}
return result;
}
var t = commonMultiple(4,6);
alert(t.toString())
t = commonDivisor(4,6);
alert(t.toString());

这个是百度知道上 很早就有人采纳的提问,你可以先搜一下,就不用再重复问题了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
没有响应m2
2013-09-16 · TA获得超过8387个赞
知道大有可为答主
回答量:7579
采纳率:33%
帮助的人:3664万
展开全部
**
*
* @return int
* @tags @param m
* @tags @param n
* @tags @return
* @todo 【方法二】利用辗除法
*/
public static int gcd(int m, int n) {
while (true) {
if ((m = m % n) == 0)
return n;
if ((n = n % m) == 0)
return m;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式