求最大公约数
最大公约数
https://ac.nowcoder.com/acm/problem/22215
gcd函数采用欧几里得算法来求解最大公约数。算法原理是通过不断将较大的数对较小的数取余,直到余数为零,此时较小的数就是最大公约数。
#include<stdio.h>
int gcd(int a,int b){//通过不断将较大的数对较小的数取余,直到余数为零,此时较小的数就是最大公约数。
while(b!=0){
int temp=b;
b=a%b;
a=temp;
}
return a;
}
int main(){
int a,b;
scanf("%d %d",&a,&b);
printf("%d",gcd(a,b));
}
