您的位置:首页 > 其它

最大公约数和最小公倍数的递归求法

2017-05-16 07:48 183 查看
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 15
#define MAXN 100005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
int gcd(int a, int b)//函数定义
{
int max = a > b ? a : b;
int min = a < b ? a : b;
a = max;
b = min;
int r = a % b;
if(0 == r)//若a能被b整除,则b就是最大公约数。
return b;
else
return gcd(b, r);//递归
}
int main()
{
//freopen("D:\\a.txt","r",stdin);
int a,b;
while(cin>>a>>b)
{
cout<<gcd(a,b)<<endl; //最大公约数
cout<<a*b/gcd(a,b)<<endl;//最小公倍数
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: