博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
845. Greatest Common Divisor
阅读量:4672 次
发布时间:2019-06-09

本文共 926 字,大约阅读时间需要 3 分钟。

描述

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);                    }}

转载于:https://www.cnblogs.com/browselife/p/10645601.html

你可能感兴趣的文章
反射,得到Type引用的三种方式
查看>>
pl sql练习(2)
查看>>
Problem B: 判断回文字符串
查看>>
谷歌浏览器,添加默认搜索引擎的搜索地址
查看>>
数据结构化与保存
查看>>
C# .net 获取程序运行的路径的几种方法
查看>>
为什么需要Docker?
查看>>
国内5家云服务厂商 HTTPS 安全性测试横向对比
查看>>
how to control project
查看>>
转 python新手容易犯的6个错误
查看>>
第四节 -- 列表
查看>>
Python入门学习笔记4:他人的博客及他人的学习思路
查看>>
webstorm里直接调用命令行
查看>>
关联规则算法之FP growth算法
查看>>
对数组序列进行洗牌
查看>>
决策树
查看>>
团队作业
查看>>
如何避免在简单业务逻辑上面的细节上面出错
查看>>
win7,Ubuntu 12.04 双系统修改启动项顺序三方法
查看>>
python--列表推导式和生成表达式
查看>>