题目题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
程序分析:利用辗除法。
代码/**
* @author :xy.hero@qq.com
* @date :Created in 2019-07-12 13:13
* @description:www.jiagou1216.com
* @modified By:
* @version: $
*/
public class Test {
public static void main(String[] args) {
int m = 6;
int n = 9;
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
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);
}
}
输出
0和3的最大公约数为3
0和3的最小公倍数为18