最大公约数(Greatest Common Divisor)
两个数的最大公约数。一个典型的解决方案是欧几里德,叫欧几里德算法。
原理:(m,n)代表m和nGCD,和m>n。然后,(m,n)=(n,m%n)=.....直到余数为0.
码如下面:
public class GCD {
public static int gcd(int m, int n){
if(m*n<0){
return -1;
}
if(n==0){
return m;
}
if(m==0){
return n;
}
//辗转相除法
if(m<n){
int temp=m;
m=n;
n=temp;
}
int r = m%n;
while(r!=0){
m=n;
n=r;
r=m%n;
}
return n;
}
public static void main(String[] args){
System.out.println(gcd(100, 45));
}
}
成绩:5
版权声明:本文博主原创文章,博客,未经同意不得转载。
最大公约数(Greatest Common Divisor)的更多相关文章
- 最大公约数Greatest Common Divisor(GCD)
一 暴力枚举法 原理:试图寻找一个合适的整数i,看看这个整数能否被两个整形参数numberA和numberB同时整除.这个整数i从2开始循环累加,一直累加到numberA和numberB中较小参数的一 ...
- LeetCode.1071-字符串最大公约数(Greatest Common Divisor of Strings)
这是小川的第391次更新,第421篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第253题(顺位题号是1071).对于字符串S和T,当且仅当S = T + ... + T ...
- 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)
定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...
- 845. Greatest Common Divisor
描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...
- hdu 5207 Greatest Greatest Common Divisor 数学
Greatest Greatest Common Divisor Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...
- upc组队赛17 Greatest Common Divisor【gcd+最小质因数】
Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...
- [UCSD白板题] Greatest Common Divisor
Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...
- greatest common divisor
One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...
- LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45
1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...
- 【Leetcode_easy】1071. Greatest Common Divisor of Strings
problem 1071. Greatest Common Divisor of Strings solution class Solution { public: string gcdOfStrin ...
随机推荐
- 为什么国外程序员爱用苹果Mac电脑?(转)
Mac 在国外很受欢迎,尤其是在 设计/web开发/IT 人员圈子里.普通用户喜欢 Mac 可以理解,毕竟 Mac 设计美观,简单好用,没有病毒.那么为什么专业人士也对 Mac 情有独钟呢?从个人使用 ...
- 使用COM提供SafeArray数据
在前一篇博文<读取SafeArray数据>我们介绍了C#读取安全数组.那么我们的COM怎么编写呢? 1. 定义SAFEARRAY变量 SAFEARRAY *pSArray = NULL; ...
- Redis集群明细文档(转)
相信很多用过Redis的同学都知道,Redis目前版本是没有提供集群功能的,只能单打独斗.如果要实现多台Redis同时提供服务只能通过客户端自身去实现.目前根据文档已经看到Redis正在开发集群功能, ...
- Android之场景桌面(一)
声明:转载请务必注明出处,本文代码和主题仅供学习交流,请勿用于商业用途. 引言:最近Android场景桌面开始流行起来了,跟原始的Android桌面相比,场景桌面能逼真的模拟各种自然物体,并且通过点击 ...
- 由sqlite在手机的内存位置,引起onCreate当运行总结
转载请注明出处.谢谢:http://blog.csdn.net/harryweasley/article/details/46467495 我们都知道,android为了操作数据库,通常是继承SQLi ...
- A Game of Thrones(2) - Catelyn
Catelyn had never liked this godswood(神木林). She had been born a Tully, at Riverrun far to the south, ...
- 泛泰A850 (高通8064+720p)刷4.4专用中文recovery TWRP2.7.1.3版
欢迎关注泛泰非盈利专业第三方开发团队 VegaDevTeam (本team 由 syhost suky zhaochengw(z大) xuefy(大星星) tenfar(R大师) loogeo cr ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第15章节--开发SP2013工作流应用程序 总结
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第15章节--开发SP2013工作流应用程序 总结 在SP2013中,工作流已经从SP Server中脱离 ...
- 《javascript高级编程》读书笔记(三)变量、范围和内存的问题
第四章:变量.范围和内存的问题 检測类型:typeof是确定一个变量是字符串.数值.布尔值,还是undefined的最佳工具.可是假设变量的值是一个对象或null,typeof仅仅会返回" ...
- String,StringBuffer以及StringBuilder的差别
1.定义: String:String 类代表字符串. Java 程序中的全部字符串字面值(如"abc" )都作为此类的实例实现. 字符串是常量:它们的值在创建之后不能更改. 字符 ...