//@time:2019.3.9
一般的算法有辗转相除,穷举,更相减损法,stein算法
不多BB,直接上算法
穷举法
int minibeishu(int a, int b)//最小公倍数
{
int temp = a > b ? a : b;
while (temp % a != 0 || temp % b != 0)
{
temp++;
}
return temp;
}
辗转相除法非递归
int Divisor(int a, int b) {//求a和b的最大公约数
int temp;
//temp = (a > b ? a : b);
//将a换成较小的数
if (a < b) {
temp = a;
a = b;
b = temp;
}
else {
temp = b;
b = a;
a = temp;
}
while (( b % a ) != 0) {
temp = b % a;
b = a;
a = temp;
}
return (a);
}
//
递归法
int gcd(int a, int b)//递归法
{
if (a < b) {//交换ab的值
a = a + b;
b = a - b;
a = a - b;
}
//求最大公约数
if (!(b % a))
return (a);
else
return gcd(b % a, a);
}
更相减损法
int gcd2(int m, int n) {//最大公因数
int i = 0, x = 1;
while (m % 2 == 0 && n % 2 == 0) {
m /= 2;
n /= 2;
i += 1;
}
if (m < n) {
m = m + n;
n = m - n;
m = m - n;
}
while (x) {
x = m - n;
m = (n > x) ? n : x;
n = (n < x) ? n : x;
if (2 * n == m)
break;
}
if (i == 0)//这里可有可无
return n;
else {
int sum = 1;
for (int i1 = 1; i1 <= i; i1++) {
sum *= 2;
}
return ((int)(sum*n));
}
}
stein算法递归
int gcd3(int a, int b) {//递归方法
if (a == b)
return a;
int big;//作为递归时的实参
int small;
if (a > b) {
big = a;
small = b;
}
else {
big = b;
small = a;
}
if (small % 2 == 0 && big % 2 == 0) {
return(2 * gcd3(big / 2, small / 2));
}
else if (big % 2 == 0) {
return gcd3(big / 2, small);
}
else if (small % 2 == 0) {
return gcd3(big, small / 2);
}
else {
return gcd3((big + small) / 2, (big - small));
}
}
stein非递归
int gcd4(int a,int b){//非递归方法
int i = 1;//作为两个都是偶数的倍数因子
while (a != b) {
if ((a % 2 != 0 && b % 2 == 0) || (a % 2 != 0 && b % 2 == 0)) {//a和b中有一个是奇数
if (a % 2 == 0) {
a /= 2;
}
else
b /= 2;
}
if (a % 2 != 0 && b % 2 != 0) {//两个都是奇数
a = a + b;
b = a - 2 * b;
}
else {//两个都是偶数
i *= 2;
a /= 2;
b /= 2;
}
}
return a;
}
主函数部分:
#include <iostream>
#include<typeinfo>//类型判断头文件
using namespace std;
int main(){
/函数声明
int Divisor(int,int);//辗转相除法非递归
int gcd(int, int);//辗转相除法递归法
int minibeishu(int, int);//穷举法求最小公倍数
int gcd2(int, int);//更相减损法
int gcd3(int, int);//stein算法递归
int gcd4(int, int);//stein算法非递归
/
int number1, number2;
cout << "请输入第一个数值" << endl;
cin >> number1;
//cout << number1 << endl;
getchar();//吸收一个回车键
while (typeid(number1) != typeid(int)) {
cout << "请输入数字而不是其他类型的数据" << endl;
cin >> number1;
if (number1 < 0)
number1 = 0 - number1;
getchar();
}
if (number1 == 0) {
cout << "请不要输入0或者其他的非int型数据" << endl;
getchar();
getchar();
return 0;
}
cout << "请输入第二个数字" << endl;
cin >> number2;
getchar();
while (typeid(number2) != typeid(int)) {
cout << "请输入数字而不是其他类型的数据" << endl;
cin >> number2;
if (number2 < 0)
number2 = 0 - number2;
getchar();
}
if (number1 == 0 || number2 == 0) {
cout << "请不要输入0或者其他的非int型数据" << endl;
getchar();
return 0;
}
int i = 0;
cout << endl;
cout << endl;
cout << "输入序号选择算法" << endl;
cout << "0号是辗转相除法非递归" << endl;
cout << "1号是辗转相除法递归" << endl;
cout << "2号是穷举法" << endl;
cout << "3号是更相减损法" << endl;
cout << "4号是stein算法的递归方式" << endl;
cout << "5号是stein算法的非递归方式" << endl;
cin >> i;
getchar();
if (i > 5 || i < 0) {
cout << "请输入一个适当的序号" << endl;
cin >> i;
}
switch (i) {
case 0:cout << Divisor(number1, number2); break;
case 1:cout << gcd(number1, number2); break;
case 2:cout << ((number1*number2) / minibeishu(number1, number2)); break;
case 3:cout << gcd2(number1, number2); break;
case 4:cout << gcd3(number1, number2); break;
case 5:cout << gcd4(number1, number2); break;
}
getchar();
return 0;
}
感谢大家。。。。。。