求最大公约数(c++)
2016-04-18 18:05
399 查看
#include <iostream> using namespace std; int greatestCommonDivisor(int a, int b) { int remain, min, max; min = (a>b) ? b : a; max = (a>b) ? a : b; remain = max%min; while (remain) { max = min; min = remain; remain = max%min; } return min; } int main(){ int a, b; cin >> a >> b; int gcd = greatestCommonDivisor(a, b); cout << gcd << endl; return 0; }
相关文章推荐
- C++ Member Functions的各种调用方式
- 用C++ 实现复杂链表的复制
- C++STL中vector容器 begin()与end()函数、front()与back()的用法
- C++中的接口继承和实现继承
- [C++]函数对象(二)
- remove_if 的用法
- 1--C语言关键字
- C++学习笔记23,类内函数重载
- vs c++内存泄露检测【模板】
- c++ sin,cos,tan,atan,atan2
- C++类的常成员函数
- OpenGL: Configuring GLFW and GLEW in Visual C++ Express
- [C++]函数对象(一)
- 编写高质量的C++代码--01 减少文件之间的编译依赖
- C++多重继承带来的问题
- C/C++内存泄漏检测
- 【c/c++】list
- c++ 字符串连接问题
- Effective C++笔记(三):资源管理
- terminate called after throwing an instance of 'std::bad_alloc