1.求解最大公约数,我们采用辗转相除法
int hcf(int a,int b)
{ int temp;
while(a%b!=0)
{ temp=a%b;
a=b;
b=temp;
}
}
由题目可知,求三个数的最大公约数,我们就需要调用hcf函数俩次,来求得最大公约数;
完整代码如下:
#include<iostream>
using namespace std;
int hcf(int a,int b)
{
int temp;
while(a%b!=0)
{temp=a%b;
a=b;
b=temp;
}
return b;}
int main()
{ int n;
cin>>n;
while(n--)
{ int a,b,c;
cin>>a>>b>>c;
int m;//用来记录返回的余数的值
m=hcf(a,hcf(b,c));
cout<<m<<endl;
}
return 0;
}