国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

算法訓(xùn)練 最大最小公倍數(shù)

2019-11-14 08:56:32
字體:
供稿:網(wǎng)友

問題描述 已知一個(gè)正整數(shù)N,問從1~N中任選出三個(gè)數(shù),他們的最小公倍數(shù)最大可以為多少。

輸入格式 輸入一個(gè)正整數(shù)N。

輸出格式 輸出一個(gè)整數(shù),表示你找到的最小公倍數(shù)。 樣例輸入 9 樣例輸出 504 數(shù)據(jù)規(guī)模與約定 1 <= N <= 106。

(PS:下面是我的代碼。)

package 最大最小公倍數(shù);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:百度了下,由于后臺(tái)測(cè)試數(shù)據(jù)出問題,所以判的只有60分) 這里寫圖片描述 (PS:下面是網(wǎng)上的AC代碼,和自己相比,自己簡(jiǎn)直low到家了。數(shù)學(xué)結(jié)論不知道,真心不知道那些參加ACM的同學(xué)是怎么挺過來的。。。)

#include<iostream>using namespace std;int main(){ long long n,ans; cin>>n; if(n<=2) ans=n; else if(n%2==1) ans=n*(n-1)*(n-2); else { if(n%3==0) ans=(n-1)*(n-2)*(n-3); else ans=n*(n-1)*(n-3); } cout<<ans<<endl; return 0;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 遂川县| 林州市| 平顶山市| 原阳县| 涟水县| 巴中市| 疏附县| 阿拉善右旗| 建瓯市| 宜良县| 财经| 平阴县| 册亨县| 清原| 五家渠市| 左贡县| 威信县| 永嘉县| 宜兰市| 商水县| 远安县| 广平县| 安庆市| 博罗县| 朝阳县| 抚顺市| 平顺县| 平谷区| 武夷山市| 岗巴县| 浏阳市| 宾川县| 吉安市| 收藏| 湘阴县| 新巴尔虎右旗| 彰武县| 重庆市| 宁德市| 遵化市| 新营市|