您的位置:首页 > 其它

辗转相除求两个数的最大公约数

2010-09-16 17:39 267 查看
第一次用C++写,欢迎大家不吝赐教

Code:

#include <iostream>

using namespace std;

class test

{

public:

test();

test(int x, int y);

~test();

int **();

private:

int a, b, temp;

};

test::test()

{

a = 0;

b = 0;

temp = 0;

}

test::test(int x, int y)

{

a = x;

b = y;

}

int test::**()

{

if (a < b)

{

temp = a;

a = b;

b = temp;

}

if (a == b)

return a;

else

{

temp = b;

while (b != 0)

{

temp = a % b;

a = b;

b = temp;

}

}

return a;

}

int main()

{

int a, b, x;

cout << "Please Input A and B :" << endl;

cin >> a >> b;

class test m(a, b);

x = m.**();

cout << a << "和" << b << "的公约数为:" << x;

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: