Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Created May 29, 2014 01:04
Show Gist options
  • Save jikeytang/d47014c29461fca417e8 to your computer and use it in GitHub Desktop.
Save jikeytang/d47014c29461fca417e8 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140529-题目1
输入两个整数n和m,从数列1,2,3......n中随意取几个数,
使其和等于m,输出其中所有的可能组合。
PS:
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
// you code
```
2. 粘贴代码时请使用shift+tab,缩进前面的空白。
@sapjax
Copy link

sapjax commented May 29, 2014

function foo(start, end, max, pre) {
    pre = pre || []
    while(start < end) {
        start++
        end-- 
        if(start < end && start <= max && end <= max) {
            var ret = pre.slice(0).concat([start, end])
            console.log(ret)
            foo(start, end - start, max, ret.slice(0, ret.length -1))
        }
    }
}


function bar(n, m) {
    foo(0, m , n < m ? n : m)
}

bar(10,10)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment