使用C++class求最大公约数

版权声明:晓程原创 https://blog.csdn.net/qq_43469554/article/details/88923843
#include <iostream>
using namespace std;
class Integer {
private:
    int _num;
public:
//构造函数
    Integer(int num) : _num(num){
    }
//计算当前Integer 和 b之间的最大公约数
    int gcd(Integer b) {
    	while (_num)
    	{
    		int t = b._num % _num;
    		b._num = _num;
    		_num = t;
		}
		return b._num;
    }
};
int main(){
    int a, b;
    cin >> a >> b;
    Integer A(a);
    Integer B(b);
    cout << A.gcd(B) << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43469554/article/details/88923843