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

很简单,但是我一开始的做法,估计会让结果越界(超过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. hdu 4358 Boring counting 离散化+dfs序+莫队算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意:以1为根节点含有N(N <= 1e5)个结点的树,每个节点有一个权值(weight ...

  2. C语言之广度优先算法

    广度优先算法又称宽度优先搜索,是一种简便的图的搜索算法之一.搜索方式大致是这样的: 直到搜索到目标结点(结点就是那些圆球球,其中有一个或者多个是目标结点)或者搜完了整个图都没找到目标结点就停止搜索. ...

  3. OFBiz之SVN下载地址

    trunk: $ svn co http://svn.apache.org/repos/asf/ofbiz/trunk ofbiz release13.07: $ svn co http://svn. ...

  4. 利用ZABBIX的RPC-JSON作API扩展应用示例

    计划将ZABBIX的一些状态可以在另一个应用的显示GRAPH及链接. 故而在网上找了几个文档,作了一个测试. https://www.zabbix.com/documentation/2.4/manu ...

  5. android ExpandableListActivity的使用

    package com.example.keKuoZhanLieBiao; import android.app.ExpandableListActivity; import android.os.B ...

  6. Altium Designer学习: 原理图和PCB元件对应查找

    画PCB的时候,需要经常的去查看原理图上对应的元件,元件数目少还好找,数目多了找起来就比较扯淡.还要Altium Designer提供了不错的交叉查找功能. 这里我建议使用两个显示器,一个显示器放原理 ...

  7. 将一个字符串映射为一个Delphi页面控件属性名(通过FindComponent和GetPropInfo找到这个控件指针)

    uses TypInfo; function TForm1.SetControlProp(ComStr, value: string): boolean; var ComName, ComProp: ...

  8. 140. Word Break II

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  9. Apache CXF 例子

    来自:http://www.cnblogs.com/frankliiu-java/articles/1641949.html Apache CXF 是一个开放源代码框架,是在Xfire 跟Celtix ...

  10. python描述符descriptor(二)

    python内置的描述符 python有些内置的描述符对象,property.staticmethod.classmethod,python实现如下: class Property(object): ...