#include<iostream>
using namespace std;
int gcd(int a,int b)
{
int t1,t2,t;
t1=a;t2=b;
while (t1) // 这里不用比较t1,t2的大小,运行时会把顺序相反的交换位置的;
{
t=t1;
t1=t2%t1;
t2=t;
}
return a/t*b; //这里不用a*b/t1;为了防止数据溢出;
}
int main()
{
int n,a,b;
while (cin>>n)
{
cin>>a;
while (--n)
{
cin>>b;
a=gcd(a,b);
}
cout<<a<<endl;
}
return 0;
}
using namespace std;
int gcd(int a,int b)
{
int t1,t2,t;
t1=a;t2=b;
while (t1) // 这里不用比较t1,t2的大小,运行时会把顺序相反的交换位置的;
{
t=t1;
t1=t2%t1;
t2=t;
}
return a/t*b; //这里不用a*b/t1;为了防止数据溢出;
}
int main()
{
int n,a,b;
while (cin>>n)
{
cin>>a;
while (--n)
{
cin>>b;
a=gcd(a,b);
}
cout<<a<<endl;
}
return 0;
}
本文详细介绍了C++中使用辗转相除法求两个数的最大公约数(GCD),并通过实例展示了其在实际编程中的应用。
414

被折叠的 条评论
为什么被折叠?



