描述

Given two numbers, number a and number b. Find the greatest common divisor of the given two numbers.

  • In mathematics, the greatest common divisor (gcd) of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers.

样例

Given a = 10, = 15, return 5.

Given a = 15, b = 30, return 15.

辗转相除法, 又名欧几里德算法(Euclidean algorithm),是求最大公约数的一种方法。它的具体做法是:用较小数除较大数,再用出现的余数(第一余数)去除除数,再用出现的余数(第二余数)去除第一余数,如此反复,直到最后余数是0为止。如果是求两个数的最大公约数,那么最后的除数就是这两个数的最大公约数。
另一种求两数的最大公约数的方法是更相减损法。
public class Solution {
/**
* @param a: the given number
* @param b: another number
* @return: the greatest common divisor of two numbers
*/
public int gcd(int a, int b) {
// write your code here if(b == 0) {
return a;
}
return gcd (b, a%b); }
}

845. Greatest Common Divisor的更多相关文章

  1. [UCSD白板题] Greatest Common Divisor

    Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...

  2. greatest common divisor

    One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...

  3. 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)

    定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...

  4. hdu 5207 Greatest Greatest Common Divisor 数学

    Greatest Greatest Common Divisor Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...

  5. LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45

    1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...

  6. 【Leetcode_easy】1071. Greatest Common Divisor of Strings

    problem 1071. Greatest Common Divisor of Strings solution class Solution { public: string gcdOfStrin ...

  7. 2018CCPC桂林站G Greatest Common Divisor

    题目描述 There is an array of length n, containing only positive numbers.Now you can add all numbers by ...

  8. upc组队赛17 Greatest Common Divisor【gcd+最小质因数】

    Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...

  9. leetcode 1071 Greatest Common Divisor of Strings

    lc1071 Greatest Common Divisor of Strings 找两个字符串的最长公共子串 假设:str1.length > str2.length 因为是公共子串,所以st ...

随机推荐

  1. mysql where和having的区别

    简单描述:需要查询一个数量count,于是做分组查询后,发现有的数据没有过滤掉,于是就想加上过滤条件,就在group by后边写了where ,发现不好使,直接就报错了,查了一下,where只能写在g ...

  2. java-HTML&javaSkcript&CSS&jQuery&ajax( 八)

    一.JavaScript教程笔记 1.在web页面中一般使用JavaScript脚本语言,支持跨平台,跨浏览器,驱动网页,与用户交互.另外Node.js把JavaScript引入到了服务器端. Jav ...

  3. 微信浏览器发送ajax请求执行多次解决方法

    http://www.cnblogs.com/whatlonelytear/p/8934738.html

  4. 安装AngularJS Batarang遇到的问题

    AngularJS Batarang是AngularJS在谷歌浏览器上的一个调试工具,因为国内目前无法访问谷歌浏览器应用商店,所以Batarang只能离线安装.不过在安装这个插件的过程中遇到了一些麻烦 ...

  5. 三维拓扑排序好题hdu3231

    /* 三维拓扑排序 将每个长方体分解成六个面,xyz三维进行操作 每一维上的的所有长方体的面都应该服从拓扑关系,即能够完成拓扑排序=如果两个长方体的关系时相交,那么其对应的三对面只要交叉即可 如 a1 ...

  6. bzoj1195 神奇的ac自动机+状态压缩dp

    /* 难的不是ac自动机,是状态压缩dp 之前做了一两题类似题目,感觉理解的还不够透彻 */ #include<iostream> #include<cstdio> #incl ...

  7. ESD选型指南

    信息来自网络,可能有误,希望指正! 一.ESD工作原理 ESD静电保护元件,又称静电抑制二极管.ESD是多个TVS晶粒或二极管采用不同的布局做成具有特定功能的多路或单路ESD保护器件,主要应用于各类通 ...

  8. JAVA 程序编译过程;编辑器,编译器和解释器

    最基本的软件工具包括,编辑器,编译器,解释器; 编译器:编译器就是将一种编程语言代码翻译成另一种语言的等效代码程序. 解释器:解释器将编译和执行交织在一起,即编译一部分代码后执行该部分代码,然后再编译 ...

  9. Django_Admin操作

    Django_Admin 创建Django_Admin 管理账户 D:\github\Django_Aadmin>python manage.py createsuperuser Usernam ...

  10. DDD实践:领域事件

    要求:修改good表,添加 organization 基础定义 用于引发和调度事件的延迟方法 AddDomainEvent Domain\SeedWork\Entity.cs public abstr ...