您的位置:首页 > 其它

9.1 ChocolatesByNumbers

2015-05-04 20:54 417 查看
N块巧克力摆成一个环从0到N-1。首先吃No.0块,然后吃掉No.M块,依次吃掉No.2M….直到遇到空的块,求可以吃掉几块。

求最小公倍数的问题。

class Solution {
public int solution(int N, int M) {
// write your code in Java SE 8
// x*M % N = y*M %N
// => n*N|(y*M - x*M) => n*N|result*M
// result = least common multiply / M
// least common multiply = N/gcd * M
int gcd = gcd(N,M);
return N/gcd;

}

static int gcd(int a, int b){
if(a%b==0) return b;
return gcd(b,a%b);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  最大公约数