李佃贵胃病网怎么预约:求c语言一道

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 06:30:41
用递归按如下公式求正整数a,b的最大公约数gcd(a,b)。
(1) 若a=b, gcd(a,b) =a
(2) 若a>b ,gcd(a,b) = gcd(a-b,b)
(3) 若a<b,gcd(a,b) = gcd(a,b-a)

#include<iostream>
using namespace std;

int max(int,int);
int small(int,int);

int main()
{
int a,b,c,d;
cin>> a>> b;

c= max(a,b);
d= small(a,b);

cout<< "max="<< c<< endl;
cout<< "small=" << d<< endl;
system("pause");
return 0;
}

int max(int x,int y)
{
int z;
for(z=x<y?x:y ; z>=1 ; z--) 求x,y中小的那个给z,如
if(x%z==0 && y%z==0) 果能整除,返回Z值。如
return z; 不能把Z减1后再比较。
}

int small(int x,int y)
{
int z;
z=max(x,y);
return (x*y/z);
}

其实楼主你的问题已经把算法都给出来了,自己加个循环不就行了