您的位置:首页 > 其它

[Templates That Are Usually Used]Greatest Common Divisor

2013-05-13 13:07 351 查看
#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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: