Amazon OA2准备——最大公约数
2015-10-18 04:28
465 查看
给一个int数组,求输出所有数字的最大公约数。题目简单明白。要是真遇上这道题就好了
使用了欧几里得算法,好像没什么特别好说的,直接给出实现好了
public static int gcd(int m, int n)
{
if(m % n == 0)
return n;
else
return gcd(n, m % n);
}
private static int gcdArray(int[] array) {
// TODO Auto-generated method stub
if(array == null)
return 0;
if(array.length == 0)
return 0;
if(array.length < 2)
return array[0];
int x = array[0];
for(int i = 1; i < array.length; i++)
{
if(array[i] > 0 && x > 0)
x = gcd(x,array[i]);
else
return 0;
}
return x;
}
使用了欧几里得算法,好像没什么特别好说的,直接给出实现好了
public static int gcd(int m, int n)
{
if(m % n == 0)
return n;
else
return gcd(n, m % n);
}
private static int gcdArray(int[] array) {
// TODO Auto-generated method stub
if(array == null)
return 0;
if(array.length == 0)
return 0;
if(array.length < 2)
return array[0];
int x = array[0];
for(int i = 1; i < array.length; i++)
{
if(array[i] > 0 && x > 0)
x = gcd(x,array[i]);
else
return 0;
}
return x;
}
相关文章推荐
- gcd
- OC多线程
- 多线程应该知道的那几件事 GCD NSThread NSOperation
- 多线程编程4 - GCD
- 多线程学习资源
- block && Grand Central Dispatch
- GCD使用指南
- 不可逆加密,数字签名,Java实现,摘要,MD5,SHA128,SHA256
- 2分钟学会GCD
- Problem 1612 Hero's gcd
- GCD学习 dispatch_barrier_async
- hdu1452因子和的积性函数
- hdu1852
- 计算两个数的最大公约数 gcd(a,b)
- 模运算相关数论知识
- The greatest common divisor gcd(最大公约数)
- Grand Central Dispatch (GCD) dispatch queues 无废话上手
- GCD 学习
- GCD介绍
- iOS的定时器用法