http://ac.jobdu.com/problem.php?pid=1439
题目描述:
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.
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int main()
{
int n;
ifstream cin("data.txt");
cin >> n;
for (; n > 0; n--)
{
int m, i, j;
long long num[100], temp;
cin >> m;
for (i = 0; i < m; i++)
cin >> num[i];
sort(num, num + m); //递增排序
temp = num[m - 1]; //取集合中的最大值
while (1)
{
for (j = 0; j < m; j++) //遍历集合中的每个元素,要求都要被最大值往上整除
if (temp % num[j] != 0)
break;
if (j == m)//都被整除了,此时temp就是最小公倍数
{
cout << temp << endl;
break;
}
else
temp += num[m - 1]; //成倍数增长,节约时间,temp++没有意义,公倍数肯定是最大值的倍数
}
}//end of for
system("pause");
return 0;
}