c ++ greatest common divisor

C ++ Euclidean algorithm obtaining the greatest common divisor

Sample input

6 9

Sample Output

3

program

#include <stdio.h>
using namespace std;
int gcd(int m,int n)
{
    if (m % n == 0)
    {
        return n;//如果他们是倍数关系,那么就是最小的数  
    }
    else
    {
        return gcd(n,m % n);//辗转相除法递归继续求  
    } 
} 
int main()
{
    int m,n;
    scanf("%d %d",&m,&n);
    printf("%d\n",gcd(m,n));
    return 0;
}

Guess you like

Origin www.cnblogs.com/LJA001162/p/11013094.html