辗除法求最大公约数与最小公倍数(java代码)

import java.util.Scanner;

public class Test32 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		int n = sc.nextInt();
		System.out.println("最大公约数:" + gongyue(m, n));
		System.out.println("最小公倍数:" + gongbei(m, n));

	}

	public static int gongyue(int m, int n) {
		if (m % n == 0)
			return n;
		if (n % m == 0)
			return m;
		if (m >= n) {
			while (m % n != 0) {
				int t = m % n;
				m = n;
				n = t;
			}
			return n;
		} else {
			while (n % m != 0) {
				int t = n % m;
				n = m;
				m = t;
			}
			return m;
		}
	}

	public static int gongbei(int m, int n) {
		return m * n / gongyue(m, n);
	}
}

 

你可能感兴趣的:(蓝桥杯)