基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
输入2个正整数A,B,求A与B的最大公约数。
Input
2个数A,B,中间用空格隔开。(1<= A,B <= 10^9)
Output
输出A与B的最大公约数。
Input示例
30 105
Output示例
15
#include"cstdio"
#include"cstring"
#include"cmath"
#include"algorithm"
using namespace std;
int GCD(int a,int b)
{
return b==0?a:GCD(b,a%b);
}
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d\n",GCD(a,b));
return 0;
}
利用辗转相除,这个中国古老的方法。然后,最主要发这个的原因是,他只需要一句话就可以求出来了。很惊喜的小玩意儿。