最大公约数Greatest Common Divisor(GCD)
一 暴力枚举法
原理:试图寻找一个合适的整数i,看看这个整数能否被两个整形参数numberA和numberB同时整除。这个整数i从2开始循环累加,一直累加到numberA和numberB中较小参数的一半为止。循环结束后,上一次寻找到的能够被两整数整除的最大i值,就是两数的最大公约数。
int getGreatestCommonDivisor(int numberA, int numberB)
{
||numberB < )
{
;
}
|| numberB <= )
{
;
}
int max = numberA > numberB ? numberA : numberB;
int min = numberA > numberB ? numberB : numberA;
)
{
return min;
}
;
;
; i <= min/; i++)
{
) && (numberB % i == ))
{
ret = i;
}
}
return ret;
}
二 辗转相除法(欧几里得算法)
原理:两个正整数a和b(a > b),它们的最大公约数等于a除于b的余数c和b之间的最大公约数。比如10和25,25除以10余数5,那么10和25的最大公约数,等于10和5的最大公约数。然后以此类推,直到两个数可以整除,或者其中一个数减小到1为止。
缺陷:当两个整形数较大时,a % b取模运算的性能比较低。
int gcd(int a, int b)
{
)
{
return b;
}
else
{
return gcd(b, a%b);
}
}
int getGreatestCommonDivisor(int numberA, int numberB)
{
||numberB < )
{
;
}
|| numberB <= )
{
;
}
int max = numberA > numberB ? numberA : numberB;
int min = numberA > numberB ? numberB : numberA;
)
{
return min;
}
else
{
return gcd(max, min);
}
}
三 更相减损术
原理:两个正整数a和b(a > b),它们的最大公约数等于a - b的差值c和较小数b的最大公约数。
缺陷:更相减损术的运算次数肯定远大于辗转相除法。特别是当两数相差比较大,相减的次数很大。
int gcd(int a, int b)
{
if (a == b)
{
return a;
}
if (a > b)
{
return gcd(a-b, b);
}
else
{
return gcd(b-a, a);
}
}
int getGreatestCommonDivisor(int numberA, int numberB)
{
||numberB < )
{
;
}
|| numberB <= )
{
;
}
int max = numberA > numberB ? numberA : numberB;
int min = numberA > numberB ? numberB : numberA;
)
{
return min;
}
else
{
return gcd(max, min);
}
}
四 辗转相除法和更相减损术相结合
原理:当a和b均为偶数,gcb(a, b) = 2 * gcb(a/2, b/2) = 2 * gcb(a>>1, b>>1) = gcb(a>>1, b>>1) << 1
当a为偶数,b为奇数,gcb(a, b) = gcb(a/2, b) = gcb(a>>1, b)
当a为奇数,b为偶数,gcb(a, b) = gcb(a, b/2) = gcb(a, b>>1)
当a和b均为奇数,先用更相减损术运算一次,gcb(a, b) = gcb(b, a-b),此时a-b是偶数,用上面的公式
说明:移位运算的性能非常快,a/2 转换成a>>1,a * 2 = a << 1
和1做&操作,判断奇偶:结果为真奇数,结果为假偶数
int gcd(int a, int b)
{
if (a == b)
{
return a;
}
)) && (!(b&))) // a和b均为偶数
{
, b>>) << ;
}
)) && (b&)) // a偶数,b奇数
{
, b);
}
) && (!(b&))) // a奇数,b偶数
{
);
}
else // a和b均为奇数
{
if (a > b)
{
return gcd(a-b, b);
}
else
{
return gcd(b-a, a);
}
return gcd(a-b, b);
}
}
int getGreatestCommonDivisor(int numberA, int numberB)
{
||numberB < )
{
;
}
|| numberB <= )
{
;
}
int max = numberA > numberB ? numberA : numberB;
int min = numberA > numberB ? numberB : numberA;
)
{
return min;
}
else
{
return gcd(max, min);
}
}
最大公约数Greatest Common Divisor(GCD)的更多相关文章
- Greatest common divisor(gcd)
欧几里得算法求最大公约数 If A = 0 then GCD(A,B)=B, since the GCD(0,B)=B, and we can stop. If B = 0 then GCD(A,B) ...
- LeetCode.1071-字符串最大公约数(Greatest Common Divisor of Strings)
这是小川的第391次更新,第421篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第253题(顺位题号是1071).对于字符串S和T,当且仅当S = T + ... + T ...
- upc组队赛17 Greatest Common Divisor【gcd+最小质因数】
Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...
- 845. Greatest Common Divisor
描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...
- [UCSD白板题] Greatest Common Divisor
Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...
- 2018CCPC桂林站G Greatest Common Divisor
题目描述 There is an array of length n, containing only positive numbers.Now you can add all numbers by ...
- CCPC2018 桂林 G "Greatest Common Divisor"(数学)
UPC备战省赛组队训练赛第十七场 with zyd,mxl G: Greatest Common Divisor 题目描述 There is an array of length n, contain ...
- 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)
定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...
- hdu 5207 Greatest Greatest Common Divisor 数学
Greatest Greatest Common Divisor Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...
随机推荐
- Android Apk反编译得到Java源代码
大家做Android开发,看到别人应用里一些好的功能,是不是很想得到源码,借鉴一下?既然Android是用JAVA开发的,那么我们就能很容易的通过反编译的到应用的源代码.下面我简单介绍下应该怎么操作. ...
- 轮播图切换 纯html+js+css
如图所示. 该图片切换特效实现很简单,而且兼容性很好. html页面如下 复制代码代码如下: <div class="wrapper"> <div id=&quo ...
- visual studio F12 失效,可能是装了插件,比如ReSharper 但是,ReSharper没有激活导致.
visual studio F12 失效,可能是装了插件,比如ReSharper 但是,ReSharper没有激活导致.
- docker 使用redis
1. 安装 centos 7 yum install docker 2. 启动 修改配置: nano /etc/sysconfig/docker 添加一下信息: OPTIONS='--selinu ...
- Why we made vorlon.js and how to use it to debug your JavaScript remotely
Vorlon.js is powered by node.JS, socket.io, and late-night coffee. I would like to share with you wh ...
- html初学(三)
<!-- 我就是我,不一样的烟花 piu piu piu 干啥子 如来神掌 -- --- ----- .======. ***********啊啊啊啊啊啊 | INRI | | | | | .= ...
- httpClenit的post出现乱码问题
在使用httpClient.executeMethod(postMethod)的时候,发现一直存在乱码问题,”book is good“被转成”book+is+good“ 返回.查看源码后,发现pos ...
- 深入理解HashMap
转自:http://annegu.iteye.com/blog/539465 Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.网上关于hashmap的文章很多 ...
- SIlverlight外包公司【技术展望】— Silverlight5.1.2最新版本发布,Silverlight 的更新从未停止。
微软发布新版Silverlight 5, 版本号5.1.20913.0 大家可以到微软网站去下载最新版本网址是 http://www.microsoft.com/getsilverlight/get- ...
- 【Hadoop测试程序】编写MapReduce测试Hadoop环境
我们使用之前搭建好的Hadoop环境,可参见: <[Hadoop环境搭建]Centos6.8搭建hadoop伪分布模式>http://www.cnblogs.com/ssslinppp/p ...