题意:求多个数的最小公倍数

很简单,但是我一开始的做法,估计会让结果越界(超过int的最大值)

import java.util.*;

import java.io.*;

public class Main{

    public static void main(String[] arg){
Scanner scan = new Scanner(new BufferedInputStream(System.in));
int n =scan.nextInt();
int[] nums = new int[1001];
while(n--!=0){
int m = scan.nextInt();
for(int i = 0 ; i != m ; i ++ ){
nums[i] = scan.nextInt();
}
int lcm = 1;
for(int i = 0 ; i != m ; i ++ ){
lcm = getLCM(lcm,nums[i]);
}
System.out.println(lcm);
}
scan.close(); } static int getLCM(int a,int b){//最小公倍数LCM为乘积除以最大公约数GCD
return b*a/getGCD(a, b);
} static int getGCD(int a,int b){//求最大公约数,辗转相除法
while(b%a!=0){
int temp = b%a;
b = a;
a = temp;
}
return a;
}; }

因此,我改动了getLCM(int a,int b)方法,避免了越界情况,结果当然是Accepted

static int getLCM(int a,int b){//最小公倍数LCM为乘积除以最大公约数GCD
b=b/getGCD(a, b);
return b*a;
}

个人感觉,这个算法还可以再优化,应该吧~

HDOJ-ACM1019(JAVA) 多个数的最小公倍数的更多相关文章

  1. Java最大公约数和最小公倍数的求法(辗转相除法)

    这道题计算了三个数的最小公倍数 import java.util.Scanner; public class D { public static int gcd(int a,int b) { int ...

  2. HDU 1019 (多个数的最小公倍数)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1019 Least Common Multiple Time Limit: 2000/1000 MS (J ...

  3. hdu 1788(多个数的最小公倍数)

    Chinese remainder theorem again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  4. n个数的最小公倍数

    Description 求n个数的最小公倍数.   Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数.   Output 为每组测试数据输出它们的最小公倍数,每个测 ...

  5. HDU_2028——求多个数的最小公倍数

    Problem Description 求n个数的最小公倍数.   Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数.   Output 为每组测试数据输出它们的最 ...

  6. hdu 1019 n个数的最小公倍数

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  7. ZZNUOJ-2154:单身狗线下聚会【求N个数的最小公倍数,会超longlong,大数乘法,Java】

    2154: 单身狗线下聚会 题目描述 马上就到七夕节了,单身狗们决定聚一聚.但是它们沉迷B站上的lo娘,他们每沉迷 ai 单身狗时间(这是它们专业计时)后就会休息 单身狗时间.它们想找到一个时间正好他 ...

  8. 求n个数的最小公倍数

    解决的问题: 对于一个长度为n序列ai,求ai的最小公倍数 解析: 我们知道,如果求两个数a,b的LCM=a*b/gcd(a,b),多个数我们可以两两求LCM,再合并,这样会爆long long 所以 ...

  9. HDU——1019Least Common Multiple(多个数的最小公倍数)

    Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

随机推荐

  1. EventLog组件

    1.使用EventLog组件读写事件日志 SourceExists方法  确定事件源是否已在本地计算机上注册 DeleteEventSource方法  用于从事件日志中移除应用程序的事件源注册 pri ...

  2. 一步步学习ASP.NET MVC3 (6)——@helper,@functions

    请注明转载地址:http://www.cnblogs.com/arhat 在前一章中,我们讲述了View如何从Action中获得数据,并显示出来,但随着需求的变化,我们可能要对View中显示的数据作出 ...

  3. 成为JavaGC专家(2)—如何监控Java垃圾回收机制

    什么是GC监控? 垃圾回收收集监控指的是搞清楚JVM如何执行GC的过程,例如,我们可以查明: 1.        何时一个新生代中的对象被移动到老年代时,所花费的时间. 2.       Stop-t ...

  4. BZOJ 2124等差子序列 线段树&&hash

    [题目描述 Description] 给一个 1 到 N 的排列{Ai},询问是否存在 1<=p1<p2<p3<p4<p5<…<pLen<=N(Len& ...

  5. CODEVS 3285 转圈游戏

    [题目描述] n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,……, ...

  6. CSS3 animation的steps方式过渡

    animation默认以ease方式过渡,它会在每个关键帧之间插入补间动画,所以动画效果 是连贯性的.除了ease,linear.cubic-bezier之类的过渡函数都会为其插入补间. 但有些效果不 ...

  7. CSS跨浏览器(转)

    本文将介绍兼容IE+.FF.Chrome.Safari.Opera的技巧 一.CSS HACK 专门为某版本的浏览器设置样式,从而解决浏览器显示的差异 selector { +property:val ...

  8. OpenVPN下载、安装、配置及使用详解

    OpenVPN下载.安装.配置及使用详解   OpenVPN简介 OpenVPN是一个用于创建虚拟专用网络(Virtual Private Network)加密通道的免费开源软件.使用OpenVPN可 ...

  9. [转载]C# HashTable 遍历与排序

    private void Form1_Load(object sender, EventArgs e) { Hashtable ht = new Hashtable(); ht.Add("j ...

  10. const变量的存储区及修改权限

    转自const变量的存储区及修改权限 [cpp] view plaincopy const int a = 1; int *p = const_cast<int*>(&a); *p ...