33 lines
743 B
Python
33 lines
743 B
Python
# 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))
|