我已经开始编写一个计算最大公约数的程序。这是我目前的代码:
#include <iostream>
#include <math.h>
using namespace std;
int getGCD(int a, int b)
{
a = a % b;
if (a == 0)
{
return b;
b = b % a;
}
if (b == 0)
{
return a;
}
}
int main()
{
int x, y;
cout << "Please enter two integers x and y, for GCD calculation" << endl;
cin >> x >> y;
cout << "The GCD of " << x << "and " << y << " is" << getGCD(x, y) << endl;
return 0;
}
我经常在GCD中得到0分。我做错了什么?
int g(int a, int b){ return b?g(b,a%b):a;}
- Lefteris E