欧几里得算法又称辗转相除法,用于求两数的最大公约数,计算公式为GCD(a,b)=GCD(b,a%b);
#include<iostream.h>
int CommFactor2(int m, int n);
int main()
{
int a, b, r;
cout<<"请输入两个自然数:";
cin>>a>>b;
//调用函数
r = CommFactor2(a, b);
cout<<a<<"和"<<b<<"的最大公约数是:"<<r<<endl;
return 0;
}
int CommFactor2(int m, int n)
{
int r = m % n;
while (r != 0)
{
m = n;
n = r;
r = m % n;
}
return n;
}