pr0518.cpp

来自「practice c++, it is from the book http:/」· C++ 代码 · 共 29 行

CPP
29
字号
//  Programming with C++, Second Edition, by John R. Hubbard
//  Copyright McGraw-Hill, 2000
//  Problem 5.18 on page 113
//  The Greatest Common Divisor Function

#include <cassert>   // defines the assert() function
#include <iostream>  // defines the cin and cout objects
using namespace std;
long gcd(long,long);

int main()
{ int m, n;
  cout << "Enter two positive integers: ";
  cin >> m >> n;
  cout << "gcd(" << m << "," << n << ") = " << gcd(m,n) << endl;
}

long gcd(long m, long n)
{ // returns the greatest common divisor of m and n:
  if (m<n) swap(m,n);
  assert(n >= 0);
  while (n>0)
  { long r=m%n;
    m = n;
    n = r;
  }
  return m;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?