您的位置:首页 > 其它

51Nod1011 最大公约数GCD

2016-12-14 21:48 246 查看
输入2个正整数A,B,求A与B的最大公约数。

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


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


Input示例
30 105


Output示例
15


#include <stdio.h>

int gcd(int a, int b){
if(!b)
return a;
return gcd(b,a%b);
}

void swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
}

int main(){
int a,b;

while(~scanf("%d%d",&a,&b)){

if(b > a)
swap(a,b);

int n = gcd(a,b);
printf("%d\n",n);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: