# 来写代码了,伪代码也行.算法题.

-1

0

import copy

input_L = [1,2,3,4]
result = []

#满足条件，输出结果
if res_sum == 0:
if x in result:
return
else:
result.append(x)
#不满足条件，返回
elif res_sum <0 or len(input_L) == 0:
return

for i in range(0, len(input_L)):
d = input_L[i]

#1 取第i个做为组合
input_L.pop(i)

#2 获取 res_sum-d 的组合结果

#3 放回i个
input_L.append(d)

print result

bluestinger1989 | 菜鸟二级 |园豆：222 | 2017-01-20 14:05

0
``` 1 package main
2
3 import "fmt"
4
5 type loc struct {
6     n int
7     i int
8 }
9
10 var cache = make(map[loc][][]int)
11
12 func main() {
13     n := 6
14     vals := []int{1, 2, 3, 4}
15     t := f(n, len(vals)-1, vals)
16     fmt.Printf("%v\n", t)
17 }
18
19 func f(n, i int, vals []int) (ret [][]int) {
20     defer func() {
21         cache[loc{n, i}] = ret
22     }()
23     t := cache[loc{n, i}]
24     if t != nil {
25         return t
26     }
27     if i < 0 {
28         return [][]int{}
29     }
30     var t1, t2 [][]int
31     if n == vals[i] {
32         t1 = [][]int{{vals[i]}}
33     }
34     if n > vals[i] {
35         t1 = f(n-vals[i], i-1, vals)
36         for k, v := range t1 {
37             t1[k] = append(v, vals[i])
38         }
39     }
40     t2 = f(n, i-1, vals)
41     return append(t1, t2...)
42 }```

go语言

Uteki | 园豆：573 (小虾三级) | 2017-01-23 10:22
0

http://stackoverflow.com/a/10629938/523533

_龙猫 | 园豆：240 (菜鸟二级) | 2017-01-24 06:54
0

0

您需要登录以后才能回答，未注册用户请先注册