问题链接:HDU-1019
问题简述
第一行输入一个整型数n,表示有n组数据,每组数据一行,每组数据包含m+1个整型数,第一个数字m表示后面有m个数字,计算出这m个数字的最小公倍数。
思路
欧几里得算法,首先算出最小公约数,然后用最小公约数可以算出最小公倍数。例如:a,b的最大公约数是c,那么a,b的最小公倍数为a*b/c。
AC通过的C++语言程序如下:
#include<iostream>
#include<vector>
int g(long long int, long long int);
int f(long long int, long long int);
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
long long int m, c, d; vector<long long int> a;
cin >> m;
for (int j = 0; j < m; j++)
{
long long int b;
cin >> b;
a.push_back(b);
}
if (m == 1) cout << a[0] << endl;
else
if (m == 2) cout << g(a[0], a[1]) << endl;
else
{
c = g(a[0], a[1]);
for (int j = 2; j < m; j++)
{
d = g(c, a[j]);
c = d;
}
cout << c << endl;
}
}
return 0;
}
int g(long long int x, long long int y)
{
return x * y / f(x, y);
}
int f(long long int x, long long int y)
{
if (y == 0) return x;
else return f(y, x%y);
}