最大公約数アルゴリズムノートは1818 codeup

直接自分のアイデアやコード、それはへの保留元のタイトル、
思考:実際にはユークリッドの設定、GCD(a、b)は= GCD(BA%のb)は

#include<stdio.h>
#include<string.h>
//#include <stdbool.h>
#include <algorithm>
using  namespace std;
//最大公约数
//写一个求最大公约数的方法
int gcd(int a,int b){
    //先判断两个数的大小,让a大于b
    int temp,i;
    if(a<b){
        temp=a;
        a=b;
        b=temp;
    }
    while(b!=0){
        i=a%b;
        a=b;
        b=i;
    }
    return a;
}
int main()
{
    //能输入多组数据
    int a,b;
    while(scanf("%d%d",&a,&b)!=EOF){
    printf("%d\n",gcd(a,b));
    }
    return 0;
}

公開された30元の記事 ウォンの賞賛4 ビュー2271

おすすめ

転載: blog.csdn.net/qq_41115379/article/details/104794398