首页 > 代码库 > CDZSC_2015寒假新人(2)——数学 A

CDZSC_2015寒假新人(2)——数学 A

A - A
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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

23 5 7 156 4 10296 936 1287 792 1
 

Sample Output

10510296
 
技术分享
 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 int LCM(int a,int b) 5 { 6     int m=a,n=b; 7     int c; 8     while(b!=0)//辗转相除法 9     { 10         c=a%b; 11         a=b; 12         b=c;13     }  14     return m/a*n;//之前写的m*n/a  结果是错的  改成这样之后终于AC了15 }16 int main()17 {18     int a,n,m,num;19     scanf("%d",&n);20     while(n--)21     {22         num=1;23         scanf("%d",&m);24         while(m--)25         {26             scanf("%d",&a);27             num=LCM(a,num);28         }29         printf("%d\n",num);30     }31 }
View Code

 

CDZSC_2015寒假新人(2)——数学 A