The greatest common divisor algorithm notes codeup 1818

The original title on hold, directly to their own ideas and code it
thinking: in fact Euclidean setting, 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;
}

Published 30 original articles · won praise 4 · Views 2271

Guess you like

Origin blog.csdn.net/qq_41115379/article/details/104794398