求n个数的最小公倍数。
Input
输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。
Output
为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。
Sample Input
2 4 6
3 2 5 7
Sample Output
12
70
#include <bits/stdc++.h>
using namespace std;
long long lcm(long long a,long long b){
if(a>b){
long long tmp=a;
a=b;
b=tmp;
}
long long ans;
for(long long i=1;i<=a;i++)
if(a%i==0&&b%i==0)
ans=i;
return a*b/ans;
}
int main(){
long long n,a[100];
long long ans;
while(~scanf("%lld",&n)){
memset(a,0,sizeof(a));
for(long long i=0;i<n;i++)
cin>>a[i];
ans=a[0];
for(long long i=0;i<n-1;i++)
ans=lcm(ans,a[i+1]);
cout<<ans<<endl;
}
return 0;
}
#include<stdio.h>
int main()
{
int x_y(int x,int y);
int i,n,a[1111],s;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
if(n==1)
printf("%d\n",a[0]);
else
{
s=x_y(a[0],a[1]);
for(i=1;i<n;i++)
s=x_y(s,a[i]);
}
printf("%d\n",s);
}
return 0;
}
int x_y(int x,int y)
{
int a,b,t,m;
a=x;b=y;
if(x>y)
{
t=x;
x=y;
y=t;}
for(;x!=0;)
{
t=y%x;
y=x;
x=t;
}
m=(a/y)*b;
return m;
}