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
被惊讶的无法分析:什么??原来这道题这么简单??为什么当时没什么人做得出来!好吧,现在冷静下来,想想。。可能是被那一大串英文吓死了吧???嗯…………看来下一次做题时。。。(嘿嘿嘿)我有一个大胆的想法。
咳咳,(表面)分析,用一个数组记录输入的要求LCM(原来那是公倍数的意思)的数,然后由小到大排序,接下来用一个变量a记录最下的那个,然后a一倍一倍的增加,知道可以被第二个数据整除为止,然后让a获得循环结束的a的值,然后用同样的方法增加a知道被第三个整除,不断循环,知道能被最大那个整除,然后记录当前的a的值,输出
ac代码
#include <iostream>
#include<string>
using namespace std;
int main()
{
int a;
cin >> a;
for (int i = 1;i <= a;i++)
{
int c;
cin >> c;
int *b = new int[c];
for (int i = 0;i < c;i++)
cin >> b[i];
for (int i = 0;i < c;i++)
{
int i1 = i;
for (int i2 = i;i2 < c;i2++)
if (b[i1] > b[i2])
i1 = i2;
int i3;
i3 = b[i];
b[i] = b[i1];
b[i1] = i3;
}
int d = b[0], e=d;
for (int i = 0;i < c-1;i++)
{
int i1 = 2;
while (d%b[i+1]!=0)
{
d = e * i1;
i1++;
}
e = d;
}
cout << e << endl;
delete b;
}
}