我已经开始这个程序来计算最大公约数。这是我迄今为止:C++程序计算最大公约数
#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;
}
我总是得到0为GCD。我究竟做错了什么?
B =%A;永远不会执行 – Mikhail
检查行返回b;并问自己,该程序如何执行b = b%a;如果你之前告诉它退出此功能。 – dowhilefor
如果这是作业,您应该添加相应的标签:) –