码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-24 14:28:17      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

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

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

Sample Output

105 10296
 
技术分享
 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

标签:

原文地址:http://www.cnblogs.com/guofeng1022/p/4245799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!