【题目描述】
从键盘任意输入两个正整数,求这两个数的最大公约数。要求使用递归的方法完成。
【输入样例】
24 36
【输出样例】
12
代码实现
#include<stdio.h>
//使用递归方法完成求最大公约数
unsigned int t[100]={1},i=0;
unsigned GetGreatestCommonDivisor(unsigned int x,unsigned int y)
{
if(t[i]==0)return t[i-1];
else
{
if(x>y)
{
i++;
t[i]=x%y;
return GetGreatestCommonDivisor(y,t[i]);
}
else if(x==y)return x;
else
{
i++;
t[i]=x%y;
return GetGreatestCommonDivisor(y,t[i]);
}
}
}
int main()
{
unsigned int x,y,z;
//输入语句
scanf("%u%u",&x,&y);
z=GetGreatestCommonDivisor(x,y);
//输出语句
printf("%u",z);
return(0);
}