辗转相除法证明与实现 最大公约数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/L1558198727/article/details/88894108

百度百科:
在这里插入图片描述

分析:

证明了a,b的公约数们, 与 a,a mod b 的公约数们是相等的集合
则需要证明两个方向

c++实现:

#include <iostream>
#include <stdio.h>
#include <string>
#include <typeinfo>
#include <stack>
#include <vector>
#include <sstream>
#include <string.h>
#include <map>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
typedef long long LL;

//递归
int gcd1(int a,int b){
    return b==0?a:gcd1(b,a%b);
}

//循环
int gcd2(int a,int b){
    int temp=0;
    while(a%b!=0){
        temp = a%b;
        a = b;
        b = temp;

    }
    return b;
}
int main()
{
    cout<<gcd1(18,12)<<endl;
    cout<<gcd2(18,12)<<endl;
    return 0;
}



猜你喜欢

转载自blog.csdn.net/L1558198727/article/details/88894108