/*Filename:gcd.cpp */
/*计算两个数的公约数*/
/*原理:反转相减法*/
#include<iostream>
using namespace std;
int gcd(int a,int b){
int big = a > b?a:b;
int small = a > b?b:a;
int result = 2;
while((result != 1)&&(result != 0)){
result = big - small;
big = result > small?result:small;
small = result > small?small:result;
}
if(result == 0){
return big;
}
return result;
}
int main(){
cout<<gcd(6,18)<<endl;
return 0;
}
转载于:https://www.cnblogs.com/candycloud/p/3360682.html