题目描述
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105.

输入
Input will consist of multiple problem instances. The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 … nm where m is the number of integers in the set and n1 … nm are the integers. All integers will be positive and lie within the range of a 32-bit integer.

输出
For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer.

样例输入

2
2 3 5
3 4 6 12

样例输出

15
12

思路:求一组数的最小公倍数,先求第一个数a和第二个数b的最小公倍数M,再将这个得到的数M与第三个数c继续求最小公倍数,直到求完最后一个数字即可。注意求M时x先除以最大公约数D再乘y,防止x乘y时发生溢出。

#include <cstdio>int gcd(int a, int b) { //求a,b的最大公约数return !b ? a : gcd(b, a % b);
}int main() {int m, n, x, y;scanf("%d", &m);while (m--) {scanf("%d", &n);scanf("%d", &x); //先输入第一个数字for (int i = 1; i < n; i++) {scanf("%d", &y);//x先除以最大公约数再乘y是为了防止溢出x = x / gcd(x, y) * y; //将x更新为x与y的最小公倍数}printf("%d\n", x);}return 0;
}

问题 A: Least Common Multiple相关推荐

  1. HDU 4913 Least common multiple

    /* hdu4913 Least common multiple http://acm.hdu.edu.cn/showproblem.php?pid=4913 离散化 线段树 统计逆序数思想 tips ...

  2. 杭电2028--Lowest Common Multiple Plus

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

  3. Lowest Common Multiple Plus ——ACM

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

  4. hdoj-2028-Lowest common multiple plus

    题目:Lowest common multiple plus 代码: #include<stdio.h> int common(int a,int b)//计算最大公约数 {int c=a ...

  5. HDOJ-1019 Least Common Multiple

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

  6. HDU2028 Lowest Common Multiple Plus【GCD+LCM】

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

  7. HDU2028 Lowest Common Multiple Plus【入门】

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

  8. Least Common Multiple

    下面为 学长 大家公认的水题, 我比水题还睡 的 WA 了三遍 才成功在好队友的帮助下,A 了 杭电HDU Least Common Multiple Problem Description The ...

  9. 1.26(Least Common Multiple)

    描述: Given A and B. Find the least positive M which is a common multiple of both A and B. 输入: Two pos ...

最新文章

  1. 《将要淘汰的八种人》读后感
  2. 【统计学习方法】朴素贝叶斯对鸢尾花(iris)数据集进行训练预测
  3. koa html中添加数据,从零开始的Koa实战(6)插入数据
  4. 给Apache虚拟主机增加端口的方法
  5. “向前进,向钱进”上:自媒体的流量变现路
  6. python除号的乘法加括号_乘除法添加括号规则
  7. mysql 建索引_mysql数据库正确建立索引及使用
  8. Linux shell script 的语法汇总
  9. Android 动态设置 layout_centerInParent
  10. java反射异常之java.lang.NoSuchFieldException: school
  11. 50 Python - 装饰器 类定义装饰器
  12. asp python 定时任务_Ubuntu使用crontab定时执行python脚本
  13. 吴恩达机器学习笔记 1单变量线性回归
  14. RecyclerView设置空视图
  15. 客户端可以连接 .Net程序不能连接SQL Server 2000
  16. iOS 中client和server的 Web Service 网络通信 (2)
  17. 65个最常见的面试问题与技巧性答复(面试技巧和注意事项),很不错,求职之前,多看看
  18. 用python制作一张简单的节日贺卡
  19. 循环双链表的p所指的结点之前插入q所指结点的操作为()
  20. 数字图像处理——12种常用图像处理方法对比

热门文章

  1. 干掉 Swagger-ui !试试这个新工具
  2. oracle计算两个日期之间相差几年几个月几天
  3. android 生成aar/jar包包含第三方jar包没有第三方jar包怎么办
  4. 18年12月英语六级选词填空
  5. html导航栏移动线的位置,编辑html,如何把导航栏固定在网页底部随屏幕移动?...
  6. Springboot项目使用jdk17启动报错:module java.base does not “opens java.lang.invoke“
  7. 特殊多位数乘法口算算法
  8. python项目之基于大数据分析算法的大学生就业系统
  9. 移植三星的uboot(三)
  10. 那些年我们一起的CSDN