Least Common Multiple

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 64855 Accepted Submission(s): 24737

Problem Description

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

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.

Output

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

Sample Input

2
3 5 7 15
6 4 10296 936 1287 792 1

Sample Output

105
10296

这道水题连续提交五次,第六次才AC。。。。。。(我真是菜,最近做做的题有点自闭)

这是是我多次修改仍然不AC的题解(测试样例全过自己测了几个也没问题,很绝望)

#include <stdio.h>
#include <math.h> int main() {

int t,temp;

scanf("%d",&t);

for(int i=1; i <= t; i++) {

int n;

scanf("%d",&n);

for(int j=1; j <= n ; j++) {

int num;

scanf("%d",&num);

if(j == 1) {

temp=num;

continue;

}

int max;

if(temp > num)

max=num;

else

max=temp;

for( ; max >= 1; max--)

if(temp%max == 0&&num%max == 0)

break;

temp=temp*num/max;

}

printf("%d\n",temp);

}

return 0;

}

最后有dalao指点,把求公因数的方法改成辗转相乘法并用函数嵌套终于AC 无奈绝望╮(╯﹏╰)╭
关于辗转相除法求最大公因数,举个栗子:
a=6, b=4; 第一步 a=a%b=6%4=2;
​ 第二步 b=4;
​ 第三步 a%b=2%4=0,此时a == 0,b=(上一步)a;
​ 此时b就是最大公因数;
(算了上详细讲解链接 https://www.cnblogs.com/shine-yr/p/5216966.html

以下为正确代码

#include <stdio.h>
#include <math.h> int gcd(int a,int b){ //辗转相除法
if(b == 0)
return a;
return gcd(b,a%b);

}

int lcm(int a,int b)

{

return a/gcd(a,b)*b; //把函数打包

}

int main() {

int t,temp,n;

scanf("%d",&t);

for(int i=1; i <= t; i++) {

scanf("%d",&n);

int temp = 1;

for(int j=1; j <= n ; j++) {

int num;

scanf("%d",&num);

temp = lcm(temp,num);

}

printf("%d\n",temp);

}

return 0;

}

(杭电1019 最小公倍数) Least Common Multiple的更多相关文章

  1. 杭电1019 Least Common Multiple【求最小公倍数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019 解题思路:lcm(a,b)=a*b/gcd(a,b) 反思:最开始提交的时候WA,以为是溢出了, ...

  2. 26最小公倍数 lowest common multiple

    题目描述 正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数. 输入描述:输入两个正整数A和B. 输出描述:输出A和B的最小公倍数. 输入例子 ...

  3. 杭电OJ 输入输出练习汇总

    主题 Calculate a + b 杭电OJ-1000 Input Each line will contain two integers A and B. Process to end of fi ...

  4. 杭电1019Least Common Multiple

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1019 题目: Problem Description The least common multiple ...

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

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

  6. HDU 1019 Least Common Multiple【gcd+lcm+水+多个数的lcm】

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

  7. Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】

    Least Common Multiple (HDU - 1019) [简单数论][LCM][欧几里得辗转相除法] 标签: 入门讲座题解 数论 题目描述 The least common multip ...

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

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

  9. ACM hdu 1019 Least Common Multiple

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

随机推荐

  1. python之内置函数,匿名函数

    什么是内置函数? 就是Python给你提供的,拿来直接用的函数,比如print,input等等.其实就是我们在创建.py的时候python解释器所自动生成的内置的函数,就好比我们之前所学的作用空间 内 ...

  2. 最近选购MP3而有感便携追求音质的一些心得

    之前的创新小石头MP3的耳机接口松动了.考虑到它已经服役了4年了.所以我准备重新买一个.而小石头出色的外放,我决定让给宝宝当玩具. 选购心得MP3的时候,原来的主导思想,是在低价位的里面考虑一台国际品 ...

  3. github发布静态页面

    github发布静态页面:https://wangc1993.github.io/2019/01/07/2/

  4. 配置 Tomcat 服务 和 自启动

    如果我们使用war 包进行部署项目的时候,需要把包放进Tomcat的目录下,为了使我们的服务能够在服务器重启的时候自动启动起来,我们需要把Tomcat设置成自起服务. 配置 Tomcat 服务 新建服 ...

  5. 36、XmlReader与 XMLWriter(抽象类)

    一.概述 XMLReader为抽象类,其派生类有:XmlDictionaryReader.XmlNodeReader.XmlTextReader(与IO命名空间中的TextReader对象一起使用). ...

  6. Foj 2299 Prefix(AC自动机、DP)

    Foj 2299 Prefix 题意 给定串s.正整数n,问有多少长度为n的字符串t满足:s[0...i]是t的子串,s[0...i+1]不是. 题解 求有多少长度为n的字符串t满足:s[0...i] ...

  7. [原]Ubuntu 下安装apache+PHP

    1.安装apache2 sudo apt-get install apache2 运行如下命令重启:sudo /etc/init.d/apache2 restart 在浏览器里输入http://loc ...

  8. 3171. [TJOI2013]循环格【费用流】

    Description 一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子.每个元素有一个坐标(行,列),其中左上角元素坐标为(0,0).给定一个起始位置(r,c) ,你可以沿着箭头防线在格 ...

  9. 查看oracle中表的索引

    oracle中表的索引信息存在 user_indexes 和 user_ind_columns 两张表里面, 其中, user_indexes 系统视图存放是索引的名称以及该索引是否是唯一索引等信息, ...

  10. Owin+ASP.NET Identity浅析系列(三)框架结构分析

    在今天,读书有时是件“麻烦”事.它需要你付出时间,付出精力,还要付出一份心境.--仅以<Owin+ASP.NET Identity浅析系列>来祭奠那逝去的…… 前两篇博客仅仅说了下功能如何 ...