-
Notifications
You must be signed in to change notification settings - Fork 0
/
p31.py
48 lines (40 loc) · 1.15 KB
/
p31.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
In England the currency is made up of pound, £, and pence, p,
and there are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
It is possible to make £2 in the following way:
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
How many different ways can £2 be made using any number of coins?
"""
"""
200 = a*1 + b*2 + c*5 + d*10+ e*20 + f*50 + g*100 + h*200
"""
class Coin(object):
def __init__(self, value, child):
self.value = value
self.child = child
def solve(self, n):
if self.child == None:
if (n != 0) and (n % self.value == 0):
return 1
else:
return 0
current = 0
for i in range(0, n+1, self.value):
if i == n:
current += 1
else:
current += self.child.solve(n-i)
return current
def number_of_ways(n):
a = Coin(1, None)
b = Coin(2, a)
c = Coin(5, b)
d = Coin(10, c)
e = Coin(20, d)
f = Coin(50, e)
g = Coin(100, f)
h = Coin(200, g)
return h.solve(200)
if __name__ == "__main__":
print(number_of_ways(200))