求两个正整数的最大公约数和最小公倍数

/*********************************************************************************
*FileName:  Test1.cpp
*Author:  Qixiang.Su
*e-mail:  617992304@qq.com
*Version:  2.0.0
*Date:  2019.3.10
*Description: 求两个正整数的最大公约数和最小公倍数
*             理解各种算法求解过程
*History:
1.Date:    2019.3.10
Author:  Elf.苏洛曦
Modification:   Create  project
2.Date:    2019.3.12
Author: Elf.苏洛曦
Modification:   1.0.0版
3.Date:    2019.3.14
Author:  Elf.苏洛曦
Modification:   2.0.0版
**********************************************************************************/
#include<iostream>
#include<time.h>
using namespace std;

int t1, t2, s, w;

//辗转相除法求最大公约数函数
int divisor(int a, int b) {
	int temp;

	//比较两个数的大小,值大的数为a,值小的数为b
	if (a < b) {
		temp = a;
		a = b;
		b = temp;
	}

	//求余
	while (b != 0) {
		temp = a % b;
		a = b;
		b = temp;
	}
	return a;
}

//求最小公倍数
int multiple(int a, int b) {
	int divisor(int a, int b);
	int temp;
	temp = divisor(a, b);
	return(a * b / temp);
}

//函数递归调用求最大公约数
int gcd(int a, int b) {
	if (a % b == 0) {
		return b;
	}
	else {
		return gcd(b, a % b);
	}
}

//穷举法求最大公约数
int divisor1(int a, int b) {
	int temp;
	temp = (a > b) ? b : a;   //求较小值
	while (temp > 0) {
		if (a % temp == 0 && b % temp == 0) {
			break;
		}
		else {
			temp--;
		}
	}
	return (temp);
}

//穷举法求最小公倍数
int multiple1(int a, int b) {
	int p, q, temp;
	p = (a > b) ? a : b;  //求两数中的最大值
	q = (a > b) ? b : a;  //求两数中的最小值
	temp = p;
	while (1) {
		if (p % q == 0) {
			break;
		}
		else {
			p += temp;
		}
	}
	return (p);
}

//更相减损法求最大公约数
int gcd1(int a, int b) {
	int i = 0, temp, x = 0;
	while (a % 2 == 0 && b % 2 == 0) {    //m,n有公约数2时
		a /= 2;
		b /= 2;
		i += 1;
	}
	//a,b的值互换
	if (a < b) {
		temp = a;
		a = b;
		b = temp;
	}
	while (x) {
		x = a - b;
		a = (b > x) ? b : x;   
		b = (b < x) ? b : x;
		if (b == (a - b)) {    //差和减数相等
			break;
		}
	}
	if (i == 0) {
		return b;
	}
	else {
		return (int)pow(2, i)*b;
	}
}

//输入正整数
int setNumber() {
	int a;
	cout << "请输入正整数:" << endl;
	cin >> a;
	if (a <= 0) {
		cout << "您输入的数值不符合规范(要求:正整数)" << endl;
		setNumber();
	}
	else {
		return a;
	}
}

//Stein算法函数非递归调用求最大公约数
int Stein(unsigned int x, unsigned int y) {
	int factor = 0;   //计数器
	int temp;

	//大数赋给x,小数赋给y
	if (x < y) {
		temp = x;
		x = y;
		y = temp;
	}
	if (0 == y) {
		return 0;
	}
	while (x != y) {
		if (x & 0x1) {
			if (y & 0x1) {   //x,y都为奇数
				y = (x - y) >> 1;
				x -= y;
			}
			else {    // x为奇数,y为偶数
				y >>= 1;
			}
		}
		else {
			if (y & 0x1) {   // x为偶数,y为奇数
				x >>= 1;
				if (x < y) {
					temp = x;
					x = y;
					y = temp;
				}
			}
			else {   //x,y均为偶数
				x >>= 1;
				y >>= 1;
				++factor;
			}
		}
	}
	return (x << factor);
}

//Stein算法函数递归调用求最大公约数
int gcd2(int u, int v) {
	if (u == 0) {
		return v;
	}
	if (v == 0) {
		return u;
	}
	if (~u & 1) {
		if (v & 1) {
			return gcd2(u >> 1, v);
		}
		else {
			return gcd2(u >> 1, v >> 1) << 1;
		}
	}
	if (~v & 1) {
		return gcd2(u, v >> 1);
	}
	if (u > v) {
		return gcd2((u - v) >> 1, v);
	}
	return gcd2((v - u) >> 1, u);
}


//获得两个正整数的最大公约数和最小公倍数
void getResult() {
	s = setNumber();
	w = setNumber();

	int count;
	cout << "请输入你想循环的次数:" << endl;
	cin >> count;
	clock_t strat = clock();
	for (int i = 0; i < count; i++) {
		t1 = divisor(s, w);
		t2 = multiple(s, w);
	}
	clock_t end = clock();
	cout << "辗转相除法函数嵌套调用所求结果:" << endl;
	cout << s << "和" << w << "的最大公约数为:" << t1 << endl;
	cout << s << "和" << w << "的最小公倍数为:" << t2 << endl;
	cout << "运行" << count << "次所花时间为:" << end - strat << "毫秒"<<endl;
	cout << endl;

	clock_t strat1 = clock();
	for (int i = 0; i < count; i++) {
		t1 = gcd(s, w);
		t2 = multiple(s, w);
	}
	clock_t end1 = clock();
	cout << "辗转相除法函数递归调用所求结果:" << endl;
	cout << s << "和" << w << "的最大公约数为:" << t1 << endl;
	cout << s << "和" << w << "的最小公倍数为:" << t2 << endl;
	cout << "运行" << count << "次所花时间为:" << end1 - strat1 << "毫秒" << endl;
	cout << endl;
		

	clock_t strat2 = clock();
	for (int i = 0; i < count; i++) {
		t1 = divisor1(s, w);
		t2 = multiple1(s, w);
	}
	clock_t end2 = clock();
	cout << "穷举法所求结果:" << endl;
	cout << s << "和" << w << "的最大公约数为:" << t1 << endl;
	cout << s << "和" << w << "的最小公倍数为:" << t2 << endl;
	cout << "运行" << count << "次所花时间为:" << end2 - strat2 << "毫秒" << endl;
	cout << endl;
		
	clock_t strat3 = clock();
	for (int i = 0; i < count; i++) {
		t1 = gcd(s, w);
		t2 = multiple(s, w);
	}
	clock_t end3 = clock();
	cout << "更相减损法所求结果:" << endl;
	cout << s << "和" << w << "的最大公约数为:" << t1 << endl;
	cout << s << "和" << w << "的最小公倍数为:" << t2 << endl;
	cout << "运行" << count << "次所花时间为:" << end3 - strat3 << "毫秒" << endl;
	cout << endl;
			
	clock_t strat4 = clock();
	for (int i = 0; i < count; i++) {
		t1 = Stein(s, w);
		t2 = multiple(s, w);
	}
	clock_t end4 = clock();
	cout << "Stein算法非递归调用所求结果:" << endl;
	cout << s << "和" << w << "的最大公约数为:" << t1 << endl;
	cout << s << "和" << w << "的最小公倍数为:" << t2 << endl;
	cout << "运行" << count << "次所花时间为:" << end4 - strat4 << "毫秒" << endl;
	cout << endl;
			
	clock_t strat5 = clock();
	for (int i = 0; i < count; i++) {
		t1 = gcd2(s, w);
		t2 = multiple(s, w);
	}
	clock_t end5 = clock();
	cout << "Stein算法递归调用所求结果:" << endl;
	cout << s << "和" << w << "的最大公约数为:" << t1 << endl;
	cout << s << "和" << w << "的最小公倍数为:" << t2 << endl;
	cout << "运行" << count << "次所花时间为:" << end5 - strat5 << "毫秒" << endl;
	cout << endl;
			
}


int main() {
	getResult();
	system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁博特

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值