思路:
如题求最大公倍数
所用函数
//求最大公因数
int gcd(int a, int b)
{
return b ? gcd(b, a%b) : a;
}
//求最大公倍数
int lcm(int a,int b)
{
//a、b相乘除以最大公约数
return a / gcd(a, b) *b;
}
代码
#include<iostream>
using namespace std;
int gcd(int a, int b)
{
return b ? gcd(b, a%b) : a;
}
int lcm(int a,int b)
{
return a / gcd(a, b) *b;
}
int main()
{
int n ,m;
cin >> n;
int temp,temp1;
for (int i = 0; i < n; i++)
{
cin >> m;
temp1 = 1;
for (int j = 0; j < m; j++)
{
cin>>temp;
temp1 = lcm(temp, temp1);
}
cout << temp1<<endl;
}
return 0;
}