原题:
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105.
题意:
给出一串数字,求出他们的最小公倍数
题解:
辗转相除法求a与b的最大公约数是指,a%b=q......r1,然后再用b%r1,直到的到余数为0,则最大公约数就是被除数,而要求a,b的最小公倍数,就是讲a*b/最大公约数。用一个函数来求前两个数的最小公倍数,然后依次与后一个数字求最小公倍数,最后得到整个数列的最小公倍数。
代码:AC
#include<iostream>
using namespace std;
long long lcm(long long a,long long b)
{
long long loss,a1,b1;
a1=a;
b1=b;
while(b>0)
{
loss=a%b;
a=b;
b=loss;
}
return (a1*b1)/a;
}
int main()
{
long long n;
cin>>n;
while(n)
{
long long m;
long long a,b,c;
cin>>m;
if(m==1)
{
cin>>c;
cout<<c<<endl;
}
else
{
cin>>c;
cin>>b;
c=lcm(c,b);
m-=2;
while(m)
{
cin>>b;
c=lcm(b,c);
m--;
}
cout<<c<<endl;
}
n--;
}
return 0;
}