1018: 函数的使用4:求最大公约数
时间限制: 1 Sec 内存限制: 128 MB
题目描述
【题意描述】
输入任一的自然数A, B, 求A , B的最大公约数
提示:推荐求最大公约数 用 辗转相除法
【输入格式】
输入两个整数A和B(1<=A,B<=2^31-1)
【输出格式】
一行一个整数,即A和B的最大公约数。
【样例输入】
51 34
【样例输出】
17
辗转相除法求最大公约数
模板题↓
#include<iostream>
#include<cstdio>
using namespace std;
int gcd(int x,int y){
if (x==0) return y;
else return gcd(y%x,x);
}
int main(){
int a,b,d;
scanf("%d%d",&a,&b);
printf("%d\n",gcd(a,b));
return 0;
}