给定两个数,求这两个数的最大公约数

例如:

输入:20 40

输出:20

下面我们采用两种方法进行解决

第一种:简单理解,但是比较繁琐

#include<stdio.h>

#include<string.h>

int   main()

{

int a,b;

scanf("%d%d",&a,&b);

if(a%b==0)

printf("%d\n",b);

else

 {

     int r=0;

     while(a%b!=0)

     {

r=a%b;

a=b;

b=r;

     }

     printf("%d\n",r);

return  0;

}

第二种:比较方便,但是相对于第一种较为“难于理解”。适用于基础较好的同学。

#include<stdio.h>

#include<string.h>

int   main()

{

int  a,b,c;

scanf("%d%d",&a,&b);

while(a%b)

{

c=a%b;

a=b;

b=c;

}

printf("%d\n",b);

return  0;

}