#include<iostream>
#include<time.h>
using namespace std;
int t1, t2, s, w;
int divisor(int a, int b) {
int temp;
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) {
a /= 2;
b /= 2;
i += 1;
}
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;
}
}
int Stein(unsigned int x, unsigned int y) {
int factor = 0;
int temp;
if (x < y) {
temp = x;
x = y;
y = temp;
}
if (0 == y) {
return 0;
}
while (x != y) {
if (x & 0x1) {
if (y & 0x1) {
y = (x - y) >> 1;
x -= y;
}
else {
y >>= 1;
}
}
else {
if (y & 0x1) {
x >>= 1;
if (x < y) {
temp = x;
x = y;
y = temp;
}
}
else {
x >>= 1;
y >>= 1;
++factor;
}
}
}
return (x << factor);
}
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");
}