思路: 求第一个和第二个元素的最小公倍数,然后拿求得的最小公倍数和第三个元素求最小公倍数,继续下去,直到没有元素
注意:通过最大公约数求最小公倍数的时候,先除再乘,避免溢出
 
 #include <iostream>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string.h>
#include <string>
#include <algorithm> using namespace std; int gcd(int a, int b)
{
return b == ? a : gcd(b, a%b);
} int main()
{
int n;
while(cin >> n)
{
while(n--)
{
int m, a, ans;
cin >> m;
cin >> a;
ans = a; // 当前的最小公倍数
while(--m)
{
cin >> a;
ans = ans * (a / gcd(ans, a)); // 这里如果先乘后除的话,可能会出现超出int限制的数。导致提交后WA
}
cout << ans << endl;
}
} return ;
}

Least Common Multiple (最小公倍数,先除再乘)的更多相关文章

  1. HDOJ 1019 Least Common Multiple(最小公倍数问题)

    Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...

  2. hdu_1019Least Common Multiple(最小公倍数)

    太简单了...题目都不想贴了 //算n个数的最小公倍数 #include<cstdio> #include<cstring> #include<algorithm> ...

  3. zoj1797 Least Common Multiple 最小公倍数

    Least Common Multiple Time Limit: 2 Seconds      Memory Limit: 65536 KB The least common multiple (L ...

  4. hdu 2028 Lowest Common Multiple Plus(最小公倍数)

    Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. 最大公约数最小公倍数 (例:HDU2028 Lowest Common Multiple Plus)

    也称欧几里得算法 原理: gcd(a,b)=gcd(b,a mod b) 边界条件为 gcd(a,0)=a; 其中mod 为求余 故辗转相除法可简单的表示为: int gcd(int a, int b ...

  6. HDU - 1019-Least Common Multiple(求最小公倍数(gcd))

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

  7. HDU1019 Least Common Multiple(多个数的最小公倍数)

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

  8. 题目1439:Least Common Multiple(求m个正数的最小公倍数lcm)

    题目链接:http://ac.jobdu.com/problem.php?pid=1439 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  9. (杭电1019 最小公倍数) Least Common Multiple

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

随机推荐

  1. es6 Promise 异步函数调用

    开发很多的时候需要异步操作,常用的做法就是用回调函数,假如需要一连串的调用,并且后面一个调用依赖前一个返回的结果的时候,就得多层嵌套回调函数,比如下面这种情况: $('.animateEle').an ...

  2. 快速傅里叶变换(FFT)学习

    首先,在写这篇博客之前,我还没有完全学会FFT. 先把会的部分打好,加深一下记忆(也可以说是做笔记吧). 初三了,还不会FFT,要退役喽-- 多项式乘法 点开这篇博客之前,你就应该知道,FFT是用来求 ...

  3. 廖雪峰Java10加密与安全-1数据安全-1加密与安全概念

    数据安全 防窃听 防篡改 防伪造 古代加密方式: 移位密码:HELLO =>IFMMP 替代密码:HELLO=>p12,5,3 现代加密方式: 建立在严格的数学理论基础上 密码学逐渐发展成 ...

  4. Django WSGI响应过程之WSGIHandler

    class WSGIHandler(base.BaseHandler): request_class = WSGIRequest def __init__(self, *args, **kwargs) ...

  5. git 命令行(二)-创建合并分支

    1. 创建和合并分支 1. 我们创建 xu 分支,然后切换到 xu 分支: 2. 用 git branch 命令查看当前分支:    git branch命令会列出所有分支,当前分支前面会标一个*号. ...

  6. MySql存储过程批量给多个数据库中的同名表添加字段

    1 创建存储过程 batchAddField:给所有"MyDB_"开头的数据库添加新字段 -- ---------------------------- -- Procedure ...

  7. 二、深入asyncio协程(任务对象,协程调用原理,协程并发)

      由于才开始写博客,之前都是写笔记自己看,所以可能会存在表述不清,过于啰嗦等各种各样的问题,有什么疑问或者批评欢迎在评论区留言. 如果你初次接触协程,请先阅读上一篇文章初识asyncio协程对asy ...

  8. FamilyFilter(4)

    3.1. 基于列族过滤数据的FamilyFilter 构造函数: FamilyFilter(CompareFilter.CompareOp familyCompareOp, ByteArrayComp ...

  9. HBase 三维模型解析

    总结下一直想写hbase的实践经验,在用hbase的过程中,我们都知道,rowkey设计的好坏,是我们能最大发挥hbase的架构优势,也是我们是否正确理解hbase的一个关键点.闲话少说,进入正题. ...

  10. VMware 设置支持UEFI

    修改后缀名为 vmx 配置文件 添加行: bios.bootDelay = "10000"      //延迟10秒进入系统 firmware = "efi" ...