蓝桥杯【历届试题 核桃的数量】 java版 求最小公倍数

这是一道求最小公倍数的题目

求最小公倍数的方法大致分为以下两类:

1、求最大公约数

     设a,b的最大公约数为(a,b),最小公倍数为[a, b],满足 a*b = (a, b) * [a, b]

2、穷举法

import java.util.Scanner;

/**
 * 核桃的数量 最小公倍数 穷举法
 * @author Sylvia
 * 2019年3月1日
 */
public class Main{
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		sc.close();
		
		int max = a;
		if(a < b) {
			if(b > c) {
				max = b;
			}else if(c > b) {
				max = c;
			}
		}else if(c > a) {
			if(c > b) {
				max = c;
			}else if(c < b) {
				max = b;
			}
		}
		int max2 = max;
		int i = 1;
		while(max2 % a != 0 || max2 % b != 0 || max2 % c != 0) {
			i++;
			max2 = max * i;       //最小公倍数一定是max的倍数,直接取max的倍数能减少循环次数,
                                  //也可以写成 max2 ++;
		}
		System.out.println(max2);
	}

}

猜你喜欢

转载自blog.csdn.net/Sylvia_17/article/details/88218284