《编程之美》---精确表达浮点数子问题---辗转相除法求取最大公约数
来源:互联网 发布:数据挖掘中的算法 编辑:程序博客网 时间:2024/06/05 20:51
辗转相除法
当两个数都较大时,采用辗转相除法比较方便.其方法是:以小数除大数,如果能整除,那么小数就是所求的最大公约数。否则就用余数来除刚才的除数;再用这新除法的余数去除刚才的余数。依此类推,直到一个除法能够整除,这时作为除数的数就是所求的最大公约数。
例如:求4453和5767的最大公约数时,可作如下除法.
5767÷4453=1余1314
4453÷1314=3余511
1314÷511=2余292
511÷292=1余219
292÷219=1余73
219÷73=3
于是得知,5767和4453的最大公约数是73。
辗转相除法适用比较广,比短除法要好得多,它能保证求出任意两个数的最大公约数。
code
/*辗转相除法求最大公约数*/#include <stdio.h>#include <stdlib.h>int GetGCD(int num1,int num2){int ys=0;int num,save;if(num1>num2){ys=num1%num2;num=num2;}else{ys=num2%num1;num=num1;}while(1){save=ys;ys=num%ys;if(ys==0){break;}else{num=save;}}return save;}int main(void){int num1,num2;int max;printf("Please enter the first number:\n");scanf("%d",&num1);printf("Please enter the sencond number:\n");scanf("%d",&num2);max=GetGCD(num1,num2);printf("The GCD is:%d\n",max);system("pause");return 0;}
0 0
- 《编程之美》---精确表达浮点数子问题---辗转相除法求取最大公约数
- 【编程之美】精确表达浮点数
- 精确表达浮点数 编程之美
- 精确表达浮点数 -- 编程之美
- 编程之美--精确表达浮点数
- 编程之美:精确表达浮点数
- 编程之美精确表达浮点数
- 编程之美2.6——精确表达浮点数
- 编程之美——精确表达浮点数
- 编程之美-2.6、精确表达浮点数
- 编程之美2:精确表达浮点数
- [编程之美] 2.6 精确表达浮点数
- 编程之美2.6 精确表达浮点数
- 【编程之美】2.6精确表达浮点数
- [编程之美] PSet2.6 精确表达浮点数
- 编程之美--2.6 精确表达浮点数
- 编程之美 2.6 精确表达浮点数
- 编程之美读书笔记2.6—精确表达浮点数
- [Android]如何导入已有的外部数据库
- pydbg学习
- wxWidgets典型错误
- 数据结构:二叉树的二叉链表存储--Java实现
- C++解析Json
- 《编程之美》---精确表达浮点数子问题---辗转相除法求取最大公约数
- Arcgis10 文件-导出地图选项没了
- 中移动强推五模手机 千元低端机面临成本难题
- 常用算法技巧及常识
- 我爱你
- 第十一讲学习,添加地名
- SSH 类库问题 java.lang.NoClassDefFoundError: Could not initialize class
- Stanford 机器学习 第二讲------- 线性回归及梯度下降
- cocos2d 粒子系统