1
2 /*Description: to
3 *
4 * Author: Vincent
5 *
6 * Date:2010/04/
7 * Contact:agilely@126.com
8 * Zju CS Lab
9
10
11
12
13 Problem Description
14 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.
15
16
17
18
19 Input
20 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.
21
22
23
24 Output
25 For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer.
26
27
28
29 Sample Input
30 2
31 3 5 7 15
32 6 4 10296 936 1287 792 1
33
34
35 Sample Output
36 105
37 10296
38
39
40 Source
41 East Central North America 2003, Practice
42
43
44
45 */
46 //1019多个数的最小公倍数
47 //先把数按从大到小排序,再两个两个求
48 #include <iostream>
49 using namespace std;
50 long LCM(long num1,long num2)
51 {
52 long int a,b,temp;
53 if(num1>0 && num2>0)
54 {
55 if(num1<num2)/*交换两个数,使大数放在num1上*/
56 {
57 temp=num1;
58 num1=num2;
59 num2=temp;
60 }
61 a=num1;b=num2;
62 while(b!=0)/*利用辗除法,直到b为0为止*/
63 {
64 temp=a%b;
65 a=b;
66 b=temp;
67 }
68 num1/=a;
69 return num1*num2;
70 }
71 else
72 { return 0;}
73 }
74 int main()
75 {
76 int t,n,ca,cb;
77 long num1,num2;
78 if(cin>>t)//用例数
79 {
80 for(ca=0;ca<t;ca++)
81 {
82 cin>>n;//每个用例中数的个数
83 cin>>num1;//第一个数
84 for(cb=1;cb<n;cb++)
85 {
86 cin>>num2;//从第二个数开始
87 num1=LCM(num1,num2);
88 }//n
89 cout<<num1<<endl;
90 }//t
91 }
92 return 0;
93 }
94
95