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); } }
相关文章推荐
- codility ChocolatesByNumbers
- codility: Euclidean algorithm ( ChocolatesByNumbers, CommonPrimeDivisors)
- Codility-ChocolatesByNumbers
- Print Numbers by Recursion
- Add two numbers represented by linked lists | Set 2
- 【12】Adds two numbers represented by a linked list and returns the sum as a linked list
- Print Numbers by Recursion
- Microsoft by the numbers
- 把两个链表加起来 add two numbers represented by two linked lists
- [剑指offer][第三章][12] Print Numbers by Recursion
- 【算法世界】(九)LeetCode -- Add Two Numbers by ListNode
- Print Numbers by Recursion
- lintcode:Print Numbers by Recursion
- Print Numbers by Recursion 解题报告
- lintcode 中等题:Print Numbers by recursion 用递归打印数字
- Add two numbers represented by linked lists
- Show line numbers by default in Vim
- Python根据内嵌的数字将字符串排序(sort by numbers embedded in strings)
- [LintCode]Print Numbers by Recursion
- 第9章-WebSocket、Socket.IO和DerbyJS的实时应用程序-9.1.WebSocket、Socket.IO和DerbyJS的实时应用程序