洛谷最大公约数和最小公倍数问题JAVA实现

功力不够;
代码行数来凑;
若有大神路过;
还请指点;

import java.util.Scanner;

public class a2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			int c = 0;
			if (a < b) {
				int twmp = a;
				a = b;
				b = twmp;
			}
			for (int i = a; i >= b; i--) {  //双重循环差点过不去
				for (int k = b; k <= i; k++) {
					if (Max(i, k) == b && Nex(i, k) == a) {//不断去重
						c++;
						break;
					}
				}
			}
			System.out.println(2 * c);
		}
	}

	public static int Max(int a, int b) {
		if (a % b == 0) {
			return b;
		}
		return Max(b, a % b);
	}

	public static int Nex(int a, int b) {
		return a * b / Max(a, b);

	}
}

你可能感兴趣的:(洛谷最大公约数和最小公倍数问题JAVA实现)