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

很简单,但是我一开始的做法,估计会让结果越界(超过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. hadoop完全分布式安装(转)

    1 安装Vmware WorkStation软件 有些人会问,为何要安装这个软件,这是一个VM公司提供的虚拟机工作平台,后面需要在这个平台上安装linux操作系统.具体安装过程网上有很多资料,这里不作 ...

  2. Python和VS

    下载VS Code 安装插件Python 安装Python,注意这里需要把Python的目录配置到环境变量中 文档结构非常重要,py文件一定位于根目录,.vscode平级:我曾经因为py文件在.vsc ...

  3. sharepoint online

    http://office.microsoft.com/en-001/sharepoint/sharepoint-online-online-collaboration-software-FX1037 ...

  4. iOS 设置代理过程

    iOS设置代理的过程 (以模拟 button 作用为例) 1.写协议 新建一个名为 MyButton 的文件,继承于 UIView,在该文件里 声明协议 myDelegate 2.写协议方法 为声明的 ...

  5. Ural1387 Vasya's Dad

    Description Vasya's dad is good in maths. Lately his favorite objects have been "beautiful" ...

  6. apt-get命令讲解

    apt-get是一条linux命令,适用于deb包管理式的操作系统,主要用于自动从互联网的软件仓库中搜索.安装.升级.卸载软件或操作系统. apt-get是debian,ubuntu发行版的包管理工具 ...

  7. hdu 3480

    斜率dp #include<cstdio> #include<cstring> #include<algorithm> #include<queue> ...

  8. 网络编程(一) 利用NSURLSession发送GET POST请求

    Xcode 7.0后,http链接不能直接访问(https可以),需要在Info.plist增加下面一项才能正确访问. 使用NSURLSession进行网络请求的流程: 1.构造NSURL 2.构造N ...

  9. ANDROID_MARS学习笔记_S02_006_APPWIDGET2_PendingIntent及RemoteViews实现widget绑定点击事件

    一.代码流程 1.ExampleAppWidgetProvider的onUpdate(Context context, AppWidgetManager appWidgetManager, int[] ...

  10. free 命令解释

    free 命令 buffers and cached 解释 N多人总是询问,当在linux在输入free时内存总数怎么加起来不一样啊,下面我来解释一下free命令的输出. 我们运行free命令时都会看 ...