原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2028
解题思路:求多个数的最小公倍数,先求第一、二个数的最小公倍数,再与第三个数求它的最小公倍数,重复此过程,第n个数则停止。即可输出结果。
<span style="white-space:pre"> </span> 注意两个32位的整数相乘会溢出。。。(不太明白数位溢出现象。。。)
#include <iostream>
using namespace std;
int lowCom(int a,int b)
{
int t1 = a,t2 = b;
while( a % b != 0 )
{
int temp = b ;
b = a%b ;
a = temp ;
}
return t1*(t2/b);//防止溢出
}
int main()
{
int n,a[10001];
while(cin >> n)
{
memset(a,0,sizeof(a));
for(int i = 1;i <= n;i++)
cin >> a[i];
int result;
if(n > 1)
result = lowCom(a[1],a[2]);
for(int i = 3;i <= n;i++)
{
result = lowCom(result,a[i]);
}
if(n == 1)
cout << a[n] << "\n";
else
cout << result << "\n";
}
return 0;
}