11154: 第六章:函数的使用《练习5:求最大公约数gcd》
Memory Limit:128 MB
Time Limit:1.000 S
Judge Style:Text Compare
Creator:
Submit:3
Solved:0
Description
【求最大公约数-辗转相除法】输入任一的自然数A, B, 求A , B的最大公约数。
输入:51 34
输出:17
#include<cstdio>
using namespace std;
int gcd(int xx,int yy)
{
if(xx==0) return yy;
else return gcd(yy%xx,xx);
}
int main()
{
int a,b,t;
scanf("%d%d",&a,&b);
printf("%d\n",gcd(a,b));
return 0;
}
Sample Input Copy
34 51
Sample Output Copy
17