最大公约数的巧妙算法

输入2个正整数A,B,求A与B的最大公约数。

Input

2个数A,B,中间用空格隔开。(1<= A,B <= 10^9)

Output

输出A与B的最大公约数。

Sample Input

30 105

Sample Output

15
#include<iostream>
#define ll long long
using namespace std;
ll gcd(ll a,ll b)
{
	return b==0?a:gcd(b,a%b);
}
int main()
{
	ll a,b;
	cin>>a>>b;
	cout<<gcd(a,b)<<endl;
	return 0;
	
 } 

猜你喜欢

转载自blog.csdn.net/xyy19990130/article/details/82056094