杭电1019-Least Common Multiple

#include<stdio.h>
int gcd(int a,int b);
int main()
{
    int n,m,a,b,i,sum;//sum是最小公倍数
    scanf("%d",&n);
       while(n--)
       {
              scanf("%d",&m);
              sum=1;//sum=1
              for(i=1;i<=m;i++)
              {
                scanf("%d",&b);
                sum=sum/gcd(sum,b)*b;
               }
               printf("%d\n",sum);
       }
     return 0;
}
int gcd(int a,int b)
{
    int temp;
    while(a%b)
    {
      temp=b;
      b=a%b;
      a=temp;  
    }
    return b;
}

时间: 2024-10-10 18:04:03

杭电1019-Least Common Multiple的相关文章

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019 解题思路:lcm(a,b)=a*b/gcd(a,b) 反思:最开始提交的时候WA,以为是溢出了,于是改成了long long,还是WA,于是就不明白了,于是就去看了discuss,发现应该这样来写 lcm(a,b)=a*gcd(a,b)*b;是为了以防a乘以b太大溢出,注意啊!!!!所以就先除再乘. #include<stdio.h> int gcd(int a,int b) { int t

杭电 2028 ( Lowest Common Multiple Plus )

链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2028 题目要求:就是求最大公倍数,我百度了一下,最好实现的算法就是: 公式法 由于两个数的乘积等于这两个数的最大公约数与最小公倍数的积.即(a,b)×[a,b]=a×b.所以,求两个数的最小公倍数,就可以先求出它们的最大公约数,然后用上述公式求出它们的最小公倍数. 例如,求[18,20],即得[18,20]=18×20÷(18,20)=18×20÷2=180.求几个自然数的最小公倍数,可以先

HDU 1019 Least Common Multiple (最小公倍数)

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30285    Accepted Submission(s): 11455 Problem Description The least common multiple (LCM) of a set of positive integers is

HDU 1019 Least Common Multiple 数学题解

求一组数据的最小公倍数. 先求公约数在求公倍数,利用公倍数,连续求所有数的公倍数就可以了. #include <stdio.h> int GCD(int a, int b) { return b? GCD(b, a%b) : a; } inline int LCM(int a, int b) { return a / GCD(a, b) * b; } int main() { int T, m, a, b; scanf("%d", &T); while (T--)

杭电1159(Common Subsequence)LCS和dp

点击打开杭电1159 Problem Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ..., xm> another sequence Z = <z1, z2, ..., zk> is a subsequence of X if there ex

hdu 1019 Least Common Multiple

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 54584    Accepted Submission(s): 20824 Problem Description The least common multiple (LCM) of a set of positive integers is

HDUJ 1019 Least Common Multiple

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29480    Accepted Submission(s): 11136 Problem Description The least common multiple (LCM) of a set of positive integers is

1019 Least Common Multiple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 51959    Accepted Submission(s): 19706   Problem Description The least common multiple (LCM) of a set of positive integers is the smallest positiv

HDU 1019 Least Common Multiple 数学题

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 prob

杭电1019

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 37254    Accepted Submission(s): 14023 Problem Description The least common multiple (LCM) of a set of positive integers is