#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int gcd(int a, int b)
{
int ans;
if(a < b)
swap(a, b);
if(b == 0) ans = a;
else
ans = gcd(b, a%b);
return ans;
}
int main()
{
int a, b, c;
cin >> a >> b;
c = gcd(a, b);
cout << c << endl;
return 0;
}
//优化:
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x%y);
}
本文介绍了一种计算两整数最大公约数(GCD)的算法,并提供了两种不同的C++实现方式,一种使用了传统的递归调用,另一种进行了简单的优化。
3万+

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



