1. 程式人生 > >藍橋杯.算法訓練:最大最小公倍數

藍橋杯.算法訓練:最大最小公倍數

藍橋杯 system.in 最大 img int color sta n-2 span

技術分享圖片

import java.util.Scanner;
public class Main {
    public void printResult(long n) {
        long result = 0;
        if(n <= 2)  //此時最多只能選擇兩個數,不符合題意
            return;
        if(n % 2 == 1) {
            result = n * (n - 1) * (n - 2);   //奇數返回n,n-1,n-2
        } else {
            if(n % 3 == 0)  //
說明n和n - 3有最大公約數3 result = (n - 1) * (n - 2) * (n - 3); //偶數12 的話 返回11,10,9 12,11,10的話會有公約數 else result = n * (n - 1) * (n - 3); //偶數返回n,n-1,n-3 } System.out.println(result); return; } public static void main(String[] args) { Main test
= new Main(); Scanner in = new Scanner(System.in); long n = in.nextLong(); test.printResult(n); } }

藍橋杯.算法訓練:最大最小公倍數