問題描述 已知一個正整數N,問從1~N中任選出三個數,他們的最小公倍數最大可以為多少。
輸入格式 輸入一個正整數N。
輸出格式 輸出一個整數,表示你找到的最小公倍數。 樣例輸入 9 樣例輸出 504 數據規模與約定 1 <= N <= 106。
(PS:下面是我的代碼。)
package 最大最小公倍數;import java.math.BigInteger;import java.util.Scanner;public class Main { public static BigInteger GCD(BigInteger a , BigInteger b){ BigInteger gcd ; while( !b.equals(BigInteger.ZERO)){ gcd = a.remainder(b); a = b; b = gcd; } gcd = a; return gcd; } public static BigInteger Max_GCM(BigInteger n){ int cnt = 0; BigInteger mul = n; BigInteger j = n.subtract(BigInteger.ONE); while(cnt != 2){ if ( GCD(mul,j).equals(BigInteger.ONE)){ mul = mul.multiply(j); cnt++; } j = j.subtract(BigInteger.ONE); } return mul; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); String str = in.next(); BigInteger n = new BigInteger(str); BigInteger TWO = new BigInteger("2"); if ( n.compareTo(TWO) == 0){ System.out.PRint(2); }else if ( n.compareTo(BigInteger.ONE) == 0){ System.out.print(1); }else if ( n.compareTo(BigInteger.ZERO) <= 0){ System.out.print(0); }else{ BigInteger max = Max_GCM(n); System.out.print(max); } in.close(); }}(PS:百度了下,由于后臺測試數據出問題,所以判的只有60分) (PS:下面是網上的AC代碼,和自己相比,自己簡直low到家了。數學結論不知道,真心不知道那些參加ACM的同學是怎么挺過來的。。。)
新聞熱點
疑難解答