题解 | #最大公约数#
最大公约数
http://www.nowcoder.com/practice/cf4091ca75ca47958182dae85369c82c
import java.util.*;
/**
* 有2周多没有练习过了,坚持练习。
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 求出a、b的最大公约数。
* @param a int
* @param b int
* @return int
*/
public int gcd (int a, int b) {
// write code here
int max = Math.max(a,b);
int min = Math.min(a, b);
if (a==b){
return a;
}
if (max%min==0){
return min;
}
int tmp = max/2;
while (true){
if(min%tmp==0 && max%tmp==0) {
return tmp;
}
tmp=tmp-1;
}
}
}
vivo公司福利 368人发布