现在的位置: 首页 > 综合 > 正文

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

2013年07月21日 ⁄ 综合 ⁄ 共 835字 ⁄ 字号 评论关闭

第一次用C++写,欢迎大家不吝赐教

Code:
  1. #include <iostream>   
  2.   
  3. using namespace std;   
  4.   
  5. class test   
  6. {   
  7. public:   
  8.     test();   
  9.     test(int x, int y);   
  10.     ~test();   
  11.     int  **();   
  12. private:   
  13.     int a, b, temp;   
  14. };   
  15. test::test()   
  16. {   
  17.     a = 0;   
  18.     b = 0;   
  19.     temp = 0;   
  20. }   
  21. test::test(int x, int y)   
  22. {   
  23.     a = x;   
  24.     b = y;   
  25. }   
  26. int  test::**()   
  27. {   
  28.     if (a < b)   
  29.     {   
  30.         temp = a;   
  31.         a = b;   
  32.         b = temp;   
  33.     }   
  34.     if (a == b)   
  35.         return a;   
  36.     else  
  37.     {   
  38.         temp = b;   
  39.         while (b != 0)   
  40.         {   
  41.             temp = a % b;   
  42.             a = b;   
  43.             b = temp;   
  44.         }   
  45.     }   
  46.     return a;   
  47. }   
  48. int main()   
  49. {   
  50.     int a, b, x;   
  51.     cout << "Please Input A and B :" << endl;   
  52.     cin >> a >> b;   
  53.     class test  m(a, b);   
  54.     x = m.**();   
  55.     cout << a << "和" << b << "的公约数为:" << x;   
  56.     return 0;   
  57. }   

 

抱歉!评论已关闭.