最大公约数和最小公倍数
2014-11-14 09:22
459 查看
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
程序分析:利用辗除法。
public class Prog6{
public static void main(String[] args){
int m,n;
try{
m = Integer.parseInt(args[0]);
n = Integer.parseInt(args[1]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("输入有误");
return;
}
max_min(m,n);
}
//求最大公约数和最小公倍数
private static void max_min(int m, int n){
int temp = 1;
int yshu = 1;
int bshu = m*n;
if(n<m){
temp = n;
n = m;
m = temp;
}
while(m!=0){
temp = n%m;
n = m;
m = temp;
}
yshu = n;
bshu /= n;
System.out.println(m+"和"+n+"的最大公约数为"+yshu);
System.out.println(m+"和"+n+"的最小公倍数为"+bshu);
}
}
程序分析:利用辗除法。
public class Prog6{
public static void main(String[] args){
int m,n;
try{
m = Integer.parseInt(args[0]);
n = Integer.parseInt(args[1]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("输入有误");
return;
}
max_min(m,n);
}
//求最大公约数和最小公倍数
private static void max_min(int m, int n){
int temp = 1;
int yshu = 1;
int bshu = m*n;
if(n<m){
temp = n;
n = m;
m = temp;
}
while(m!=0){
temp = n%m;
n = m;
m = temp;
}
yshu = n;
bshu /= n;
System.out.println(m+"和"+n+"的最大公约数为"+yshu);
System.out.println(m+"和"+n+"的最小公倍数为"+bshu);
}
}
相关文章推荐
- 最大公约数与最小公倍数
- JS求最小公倍数(高效算法2—利用最大公约数和递归调用)
- 最小公倍数与最大公约数问题(NOIP竞赛原题)
- 最大公约数和最小公倍数
- 求两个整数的最小公倍数与最大公约数
- 最大公约数和最小公倍数问题(天梯)
- 常见算法:C语言求最小公倍数和最大公约数三种算法
- 8.最大公约数和最小公倍数问题
- 输入两个整数,求其最大公约数和最小公倍数
- C语言求最小公倍数和最大公约数三种算法
- Console-算法[if,while]-一输入两个正整数m和n,求其最大公约数和最小公倍数
- 常见算法:C语言求最小公倍数和最大公约数三种算法
- 最大公约数与最小公倍数————多种方法
- 最大公约数和最小公倍数
- 最大公约数,最小公倍数
- 最大公约数 & 最小公倍数 计算
- 最小公倍数,最大公约数
- 最大公约数和最小公倍数
- 用Java实现最大公约数与最小公倍数
- 常见算法:C语言求最小公倍数和最大公约数三种算法