题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2028
Lowest Common Multiple Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 37117 Accepted Submission(s): 15248
Problem Description
求n个数的最小公倍数。
Input
输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。
Output
为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。
Sample Input
2 4 6 3 2 5 7
Sample Output
12 70
Author
lcy
Source
Recommend
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 110;
int Gcd(int a, int b)
{
if(!b)
return a;
return Gcd(b, a%b);
}
int main()
{
int arr[MAXN], iNum, n, i;
while(~scanf("%d", &n))
{
for(i = 0; i < n; ++i)
scanf("%d", &arr[i]);
iNum = arr[0];
for(i = 1; i < n; ++i)
iNum = iNum / Gcd(iNum, arr[i]) * arr[i];//先除后乘,这样可以防止溢出
printf("%d\n", iNum);
}
return 0;
}