Rebased projecteuler folder, now includes all contest programming stuff
This commit is contained in:
32
projecteuler/031/main.py
Normal file
32
projecteuler/031/main.py
Normal file
@@ -0,0 +1,32 @@
|
||||
# Coin sums
|
||||
|
||||
# You have coins for 1p, 2p, 5p, 10p, 20p, 50p, 100p and 200p
|
||||
# Find out how many ways there are to pay 200p
|
||||
|
||||
def count(S, n, N):
|
||||
|
||||
if N == 0:
|
||||
return 1
|
||||
|
||||
if N < 0 or n < 0:
|
||||
return 0
|
||||
|
||||
# Case 1. include current coin S[n] in solution and recur
|
||||
# with remaining change (N - S[n]) with same number of coins
|
||||
incl = count(S, n, N - S[n])
|
||||
|
||||
# Case 2. exclude current coin S[n] from solution and recur
|
||||
# for remaining coins (n - 1)
|
||||
excl = count(S, n - 1, N)
|
||||
|
||||
# return total ways by including or excluding current coin
|
||||
return incl + excl
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
S = [1, 2, 5, 10, 20, 50, 100, 200]
|
||||
|
||||
N = 200
|
||||
|
||||
print("Total number of ways to get desired change is", count(S, len(S) - 1, N))
|
||||
Reference in New Issue
Block a user