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 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

 

题意:

第一次输入n代表有N组测试用例,第二次输入的m代表求m个数的最小公倍数。

 

代码:

 #include<stdio.h>
    int g(int a,int b);
    int main()
    {
        int n,m,i,j,t;
        int  sum;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            int a[1000]={0};
            scanf("%d",&m);
            for(j=0;j<m;j++)//可以每输入一个数做一次处理,代码可以更简洁。但是我还是喜欢用数组- -!
            {
                scanf("%d",&a[j]);
            }
            t=a[0] ;
            sum=a[0];
            for(j=1;j<m;j++)
            {
                sum=sum/g(t,a[j])*a[j];//这个地方需要注意先除后成,否者可能会超出数据范围
                t=sum;
            }
            printf("%d\n",t);
        }
        return 0;
    }

    int g(int   a,int   b)
    {
        if(a%b==0)
        return b;
        else
        return g(b,a%b);
    }


 

分析:

求N个数的最小公倍数,只要先求前n-1个数的最小公倍数然后再跟第n个数求最小公倍数就可以了,

还有就是要注意先除后乘

 

你可能感兴趣的:(测试,Integer,input,each,output,Numbers)