1806: n个数的最小公倍数
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 188 Solved: 100
Submit Status Web Board
Description
给定n个数字,求这n个数字的最小公倍数。
Input
输入分两行,第一行是一个n,代表有n个数字,接着第二行输入n个数字,数字均在int范围内
Output
输出这n个数字的最小公倍数,保证结果在int范围内
Sample Input
3
1 2 3
Sample Output
6
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
#define N 1010
#define ll long long
using namespace std;
int a[N];
ll gcd(ll x,ll y)
{
ll t;
if(x<y){t=x;x=y;y=t;}
return y?gcd(y,x%y):x;
}
int main()
{
int n,i,j;
ll sum;
while(scanf("%d",&n)!=EOF)
{
sum=1;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum=sum*a[i]/gcd(sum,a[i]);
}
printf("%lld\n",sum);
}
return 0;
}