求两个正整数a 和 b的最大公约数。
要求使用c++ class编写程序。可以创建如下class
/* students please write your program here */
#include <iostream>
using namespace std;
class Integer {
private:
int _num;
public:
//构造函数
Integer(int num) {
_num = num;
}
//计算当前Integer 和 b之间的最大公约数
int gcd(Integer b)
{
int min = _num < b._num ? _num : b._num;
int max = _num > b._num ? _num : b._num;
for (int i = min; i >= 1; i--)
{
if (max%i == 0 && min%i==0)
{
return i;
}
}
}
};
int main() {
int a, b;
cin >> a >> b;
Integer A(a);
Integer B(b);
cout << A.gcd(B) << endl;
return 0;
}