最大公约数(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 ...
随机推荐
- Ajaxterm
Index of /software/ajaxterm Ajaxterm Since Mon Feb 28 03:22:42 CET 2011, hosted here: github.com/ant ...
- Hdu 4539 【状态DP】.cpp
题意: 一个炮兵可以攻打和他之间曼哈顿距离为2的士兵,给出你一块n*m的战场,告诉你哪些地方可以站人哪些地方不可以,问你最多可以安放多少个士兵? n <= 100, m <= 10 思路: ...
- Spark技术内幕:Stage划分及提交源代码分析
当触发一个RDD的action后.以count为例,调用关系例如以下: org.apache.spark.rdd.RDD#count org.apache.spark.SparkContext#run ...
- WPF界面设计技巧(5)—自定义列表项呈现内容
原文:WPF界面设计技巧(5)-自定义列表项呈现内容 接续上次的程序,稍微改动一下原有样式,并添加一个数据模板,我们就可以达成下面这样的显示功能: 鼠标悬停于文件列表项上,会在工具提示中显示图像缩略图 ...
- SWT的TitleAreaDialog详解
转自:http://www.cnblogs.com/AllenYoung/archive/2006/10/05/521805.html Dialog是SWT和JFace的一个重要的组成部分,我们在开发 ...
- Android - 通过Intent启动Activity
通过Intent启动Activity 本文地址: http://blog.csdn.net/caroline_wendy 为了动态关联Activity界面,使用Intent启动.能够灵活绑定. 在In ...
- 教你怎么去一个APP的JSON数据,你懂的
今天闲着没事.谁让我今天是光棍节呢,算给大家一个福利.没事逛着应用市场.想找个应用高仿下,突然发现一个应用竟然跟我一个名字尼玛,尼玛应用界面做的非常easy.我认为应该不难. 惯性操作想去破解APK. ...
- <EditText /> This text field does not specify an inputType or a hint
我是一个警告,xml代码是: <EditText android:id="@+id/str_ipaddress" android:layout_width="wra ...
- 打开或导入项目,从脱机 Outlook 数据文件 (.ost)
打开或导入项目,从脱机 Outlook 数据文件 (.ost) Microsoft Outlook 2010 doesn\rquote t 支持手动打开或导入项目,从一个 脱机 Outlook 数据文 ...
- UVALive - 4621 Cav 贪心 + 分析
题目大意:有一张洞穴地图,要在这个洞穴里面存放水,要求水不能碰到洞穴顶部.如今给出每一个位置的顶部位置和地面高度.问最多能够放多少水 解题思路:根据物理定理,每一段有水的连续区间,水位高度必须相等 所 ...