62,614
社区成员
发帖
与我相关
我的任务
分享
//【程序6】 有问题!!!!!!!!!!!!!!!!!!!!!!
//题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
//1.程序分析:利用辗除法。
//如何用辗转相除法求最小公倍数???
class Function
{
private int m;
private int n;
private int yue;
private int bei;
Function(int m,int n)
{
this.m = m;
this.n = n;
yue = 0;
bei = 0;
}
/*
被除数 / 除数 = 商 ...... 余数
6497 / 3869 = 1 ...... 2628
3869 / 2628 = 1 ...... 1241
2628 / 1241 = 2 ...... 146
1241 / 146 = 8 ...... 73
146 / 73 = 2 ...... 0
因此最大公约数为:73
*/
public int getYue()
{
while(m%n !=0 || n%m !=0)
{
if (m>n && m%n !=0)
{
m = m % n;
}
else if (n>m && n%m !=0)
{
n = n % m;
}
else
{
break;
}
yue = (m>n)?n:m;
}
return yue;
}
//最小公倍数=两数之积/最大公约数=6497*3869/73=25136893/73=34431
public int getBei()
{
bei = m*n/(new Function(m,n).getYue());
return bei;
}
}
class Programme6
{
public static void main(String[] args)
{
Function f = new Function(6497,3869);
//new Function(6497,3869).getYue()
System.out.println("最大公约数为:"+f.getYue());
//new Function(6497,3869).getBei()
System.out.println("最小公倍数为:"+f.getBei());
}
}