天天看點

藍橋杯——最大最小公倍數

資源限制

時間限制:1.0s 記憶體限制:256.0MB

問題描述

已知一個正整數N,問從1~N中任選出三個數,他們的最小公倍數最大可以為多少。

輸入格式

輸入一個正整數N。

輸出格式

輸出一個整數,表示你找到的最小公倍數。

樣例輸入

9

樣例輸出

504

資料規模與約定

1 <= N <= 106。

import java.util.Scanner;

public class Main {
//    public static int gcd(int a,int b){  //最小公約數
//    	return b == 0 ? a : gcd(b, a % b);
//    }
//     public static int lcm(int a,int b){	//最大公倍數
//    	   return a * b / gcd(a,b);
//    }
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long ans;
//		暴力 逾時........
//		for(int i = n; i >= 2; i--) {
//			for(int j = i-1; j >= 2; j--) {
//				for(int k = j - 1; k >= 2; k--) {
//					if(i != j && i != k && max < lcm(lcm(k,j),i)) {
//						max = lcm(lcm(k,j),i);
//					}
//				}
//			}
//		}
		if(n <= 2) {  //小于等于2 
			ans = n;
		}else if(n % 2 == 1) {	//奇數 最小公倍數的最大值為末尾的三個數相乘  9
			ans = n * (n-1) * (n-2);	
		}else if(n % 3 == 0) {	//偶數 不是倍數  如8
			ans = (n-1) * (n-2) * (n-3);		
		}else {						//偶數 是三的倍數  如6
			ans = n * (n-1) * (n-3);
		}
		System.out.print(ans);
	}
	
}