# C++求最大公约数四种方法解析

C++求最大公约数的四种方法思路，供大家参考，具体内容如下

```#include
using namespace std;
int CommFactor1(int m, int n); //函数的声明
int main()
{
int a, b;
cin >> a >> b;
cout << "这两个数的最大公约数为：" << CommFactor1(a,b)<< endl;
return 0;
}
int CommFactor1(int m,int n)
{
int tem;
for (tem = m;; tem--)
{
if (m % tem == 0 && n % tem == 0)
{
break;
}
}
return tem;
}```

```#include
using namespace std;
int CommFactor2(int m, int n); //函数的声明
int main()
{
int a, b;
cin >> a >> b;
cout << "这两个数的最大公约数为：" << CommFactor2(a,b)<< endl;
return 0;
}
int CommFactor2(int m,int n)
{
int i;
int factor = 1;
for (i=2;i<=m&&i<
```

```#include
using namespace std;
int CommFactor3(int m, int n); //函数的声明
int main()
{
int a, b;
cin >> a >> b;
cout << "这两个数的最大公约数为：" << CommFactor2(a,b)<< endl;
return 0;
}
int CommFactor3(int m,int n)
{
int z = n;
while (m % n != 0)
{
z = m % n;
m = n;
n = z;
}
return z;
}```

```#include
using namespace std;
int CommFactor4(int m, int n); //函数的声明
int main()
{
int a, b;
cin >> a >> b;
cout << "这两个数的最大公约数为：" << CommFactor4(a,b)<< endl;
return 0;
}
int CommFactor4(int m,int n)
{

while (m != n)
{
if (m > n)
{
m = m - n;
}
else
{
n = n - m;
}
}
return m;
}```