Lowest Common Multiple PlusTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25071 Accepted Submission(s): 10147
Problem Description
求n个数的最小公倍数。
Input
输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。
Output
为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。
Sample Input
Sample Output
Author
lcy
Source
Recommend
lcy
|
=======================================代码=======================================
水题直接贴代码。
#include<stdio.h>
int SteinGcd(int A,int B) //求Gcd的Stein算法
{
int cnt=0;
while(B)
{
if(A<B) { int tmp=A; A=B; B=tmp; }
if(A&B&1) { A=(A+B)>>1; B=A-B; } //a and b are odd
else if(A&1) B>>=1; //only a is odd
else if(B&1) A>>=1; //only b is even
else { A>>=1; B>>=1; ++cnt; } //a and b are even
}
return A<<cnt;
}
int main()
{
int N;
while(scanf("%d",&N)==1)
{
unsigned int ans=1,tmp; //注意要使用unsigned int保存结果ans
while(N--)
{
scanf("%d",&tmp);
ans*=tmp/SteinGcd(ans,tmp);
}
printf("%d\n",ans);
}
return 0;
}