输入两个正整数,求其最大公约数。
输入描述:
测试数据有多组,每组输入两个正整数。
输出描述:
对于每组输入,请输出其最大公约数。
示例1
输入
49 14
输出
7
题解
求最大公约数其实是一个a mod b 不断迭代的过程
直到最后mod零,输出他本身
于是定义函数GCD
c++11
#include<iostream>
#include<cstdio>
using namespace std;
int GCD(int a,int b){
if(b==0){
return a;
}else{
return GCD(b,a % b);
}
}
int main(){
int a, b;
cin>>a>>b;
cout<<GCD(a,b)<<endl;
}