Recently, I started to learn a method, and share it with Xiaobai like me
(Maybe the writing is not very good, if you find any deficiencies, please give suggestions)
The idea of the picture is as follows:
Ideas:
1. Compare the size of the two (such as a>b)
code show as below:
int a=0;
int b=0;
printf("输入a和b的值:>");
scanf("%d%d",&a,&b);
int max=(a>b?a:b);//最大值
int min=(a<b?a:b);//最小值
2. Maximum value divided by minimum value
code show as below:
max%min==0
3. When the divisor is 0, the greatest common divisor is the minimum value
The whole code is as follows:
#include<stdio.h>
int Add(int a,int b)
{
int max=(a>b?a:b);//最大值
int min=(a<b?a:b);//最小值
if(max%min==0)
return min;
return Add((max%min),min);
}
int main()
{
int a=0;
int b=0;
printf("输入a和b的值:>");
scanf("%d%d",&a,&b);
Add(a,b);//返回最大公约数
printf("%d",Add(a,b));
return 0;
}