蓝桥杯 最大最小公倍数(java题解)

来源:互联网 发布:宜家 知乎 编辑:程序博客网 时间:2024/06/07 07:22

问题描述
已知一个正整数N,问从1~N中任选出三个数,他们的最小公倍数最大可以为多少。

输入格式
输入一个正整数N。

输出格式
输出一个整数,表示你找到的最小公倍数。
样例输入
9
样例输出
504
数据规模与约定
1 <= N <= 106。

题解:

import java.util.*;import java.math.*;public class Main {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        int n = in.nextInt();        if(n<1 || n>1000000)             System.exit(0);        if(n<=2)            System.out.println(2);        BigInteger a = BigInteger.valueOf(n-1);        BigInteger b = BigInteger.valueOf(n-2);        BigInteger c = BigInteger.valueOf(n-3);        BigInteger x = BigInteger.valueOf(n);        BigInteger res;        if(n%2!=0) {            res = x.multiply(a).multiply(b);            System.out.println(res);        }        else if(n%2==0 && n%3!=0) {            res = x.multiply(a).multiply(c);             System.out.println(res);        }        else {            res = a.multiply(b).multiply(c);            System.out.println(res);        }        in.close();    }}
0 0
原创粉丝点击