求取两个整数的最大公约数算法
2017-03-18 23:22
489 查看
#include "stdafx.h" #include<stdio.h> #include<string> #include<iostream> using namespace std; int algorithm1(int max, int min)//反复取余法 计算最大公约数 { int x; if ((x=max%min) != 0)//当余数为0时,得到最大公约数 { return algorithm1(min, x);//,余数不为0时,递归调用,反复计算直到余数为0结束 } else { return min;//余数为0时求得最大公约数 } } int algorithm2(int max, int min)//反复相减法得最大公约数 { int x; x = max - min;//求两参数之差x if (x != 0)//参数是否相等 { if (max%x == 0 && min%x == 0)//当x能同时整除两个参数时,x为最大公约数 { return x; } else { if (x > min)//使用递归,直到得到最大公约数 { return algorithm2(x, min); } else { return algorithm2(min, x); } } } else return max;//两参数相等时返回max } int algorithm3(int max,int min)//反复对差取余法求最大公约数 { int y = max - min;//求两参数之差 if (y != 0) { int x = max%y;//用参数m 4000 ax对差求余 if (x == 0) return y;//若为0则得到最大公约数,返回 else return algorithm3(y, x);//使用递归 } else return max;//若两参数相等则返回max } int algorithm4(int max, int min)//穷举法 { for (int i = min;i >=1;i--)//从较小的参数递减,递减后若能同时被两参数整除,则为最大公约数 { if (min%i == 0 && max%i == 0) return i; else continue; } } int algorithm5(int x, int y)//stein算法 { int temp; if (x < y) { temp = x; x = y; y = temp; } if(0==y) { return x; } if (x % 2 == 0 && y % 2 == 0) return 2 * algorithm5(x / 2, y / 2); else if (x % 2 == 0) return algorithm5(x / 2, y); else if (y % 2 == 0) return algorithm5(x, y / 2); else return algorithm5((x + y) / 2, (x - y) / 2); } bool match(string s)//判断输入数据是否合法 { char c; for (int i = 0;i < s.length();i++)//对s中的字符逐一检查 { c = s.at(i); if (c<'0' || c>'9') return false; } return true; } void maininterface()//主界面 { cout << "***************************************"<<endl; cout << "***** 算法1、反复取余算法 *****"<<endl; cout << "***** 算法2、反复相减算法 *****"<<endl; cout << "***** 算法3、反复对差取余算法 *****"<<endl; cout << "***** 算法4、穷举算法(验证) *****"<<endl; cout << "***** 算法5、stein算法 *****"<<endl; cout << "***** 6、退出程序 *****"<<endl; cout << "***************************************" << endl; } void main() { string choose;//算法选择 string m;//输入的第一个数字 string n;//输入的第二个数字 while(1) { maininterface(); cout << "请输入算法序号(1~6):"; cin >> choose; cout << endl; if (!match(choose))//判断输入是否为数字 { cout << "输入错误,请输入数字1~6"<<endl; continue; } if (atoi(choose.c_str()) != 6)//将数字字符串转为数字,为6时退出程序 { cout << "请输入第一个数:"; cin >> m; cout << endl; cout << "请输入第二个数:"; cin >> n; cout << endl; if (match(m) && match(n))//输入数字m,n是否正确 { int max = atoi(m.c_str());//将m转为整型数字 int min = atoi(n.c_str());//将n转为整型数字 int temp; if (max < min) { temp = max; max = min; min = temp; } switch (atoi(choose.c_str()))//选择计算算法 { case 1: cout << "最大公约数为:" << algorithm1(max, min) << endl;break; case 2: cout << "最大公约数为:" << algorithm2(max, min) << endl;break; case 3: cout << "最大公约数为:" << algorithm3(max, min) << endl;break; case 4: cout << "最大公约数为:" << algorithm4(max, min) << endl;break; case 5: cout << "最大公约数为:" << algorithm5(max, min) << endl;break; default: cout << "数字输入错误,请重新输入:"; cout << endl; break; } } else cout << "数字格式错误,请重新输入" << endl; } else { cout << "程序结束"<<endl; return; } } }
相关文章推荐
- 求两个整数的最大公约数的各种算法(C语言实现)
- 算法实现求两个整数的最大公约数!
- java小算法—输入两个正整数m和n,求其最大公约数和最小公倍数
- 求两个整数的最大公约数的各种算法(C语言实现)
- php计算两个整数的最大公约数常用算法小结
- 时间复杂度最小的 求两个整数最大公约数算法
- php计算两个整数的最大公约数常用算法小结
- 欧几里得求两个整数最大公约数算法的汇编递归实现代码
- iOS 算法面试题(打印2-100之间的素数、求两个整数的最大公约数、查找字符串的位置)~demo
- 求两个整数的最小公倍数和最大公约数的算法及其C++实现
- C#实现用欧几里德算法、连续整数检测算法、公因数算法求两个非负整数的最大公约数
- 求两个整数的最大公约数,算法原理辗转相除法 原理: GCD (x,y) = GCD(y,x%y)
- php取两个整数的最大公约数算法大全
- Console-算法[if,while]-一输入两个正整数m和n,求其最大公约数和最小公倍数
- php取两个整数的最大公约数算法大全
- 使用三种算法解决从键盘输入两个整数,输出两个整数的最大公约数的问题
- 输入两个正整数m和n,求其最大公约数和最小公倍数
- 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
- 求两个整数的最大公约数
- C语言_输入两个正整数m和n,求其最大公约数和最小公倍数。