回答編集履歴
2
追記
answer
CHANGED
@@ -15,4 +15,27 @@
|
|
15
15
|
ans = coin_sums2(200, len(coins)-1)
|
16
16
|
print(ans)
|
17
17
|
|
18
|
+
```
|
19
|
+
|
20
|
+
---
|
21
|
+
|
22
|
+
下の方法だと元のコードのロジックを活かせます。
|
23
|
+
```python
|
24
|
+
def coin_sums2(remainder, coin):
|
25
|
+
total = 0
|
26
|
+
def coin_sums_i(remainder, coin):
|
27
|
+
nonlocal total
|
28
|
+
if coin < 0:
|
29
|
+
total += 1
|
30
|
+
return
|
31
|
+
n = remainder // coins[coin]
|
32
|
+
for i in range(n + 1):
|
33
|
+
coin_sums_i(remainder - i * coins[coin], coin - 1)
|
34
|
+
|
35
|
+
coin_sums_i(remainder, coin)
|
36
|
+
return total
|
37
|
+
|
38
|
+
coins = [2, 5, 10, 20,50, 100, 200]
|
39
|
+
ans = coin_sums2(200, len(coins)-1)
|
40
|
+
print(ans)
|
18
41
|
```
|
1
修正
answer
CHANGED
@@ -1,17 +1,18 @@
|
|
1
|
-
|
1
|
+
けっきょくtotalは作らなくても良さそうでした。
|
2
2
|
|
3
3
|
```python
|
4
|
-
def coin_sums2(remainder, coin
|
4
|
+
def coin_sums2(remainder, coin):
|
5
5
|
if coin < 0:
|
6
|
-
return
|
6
|
+
return 1
|
7
|
+
else:
|
7
|
-
|
8
|
+
n = remainder // coins[coin]
|
9
|
+
results = []
|
10
|
+
for i in range(n + 1):
|
11
|
+
results.append(coin_sums2(remainder - i * coins[coin], coin - 1))
|
12
|
+
return sum(results)
|
8
13
|
|
9
|
-
for i in range(n + 1):
|
10
|
-
total += coin_sums2(remainder - i * coins[coin], coin - 1, total)
|
11
|
-
return total
|
12
|
-
|
13
14
|
coins = [2, 5, 10, 20,50, 100, 200]
|
14
|
-
ans = coin_sums2(200, len(coins)-1
|
15
|
+
ans = coin_sums2(200, len(coins)-1)
|
15
16
|
print(ans)
|
16
17
|
|
17
18
|
```
|