Problem 31
In England and the currency is made up of pound, round, and pence, P, and there are eight coins in general circulation:
1 p, 2 p, 5 p, 10 p, 20 p, 50 p, half 1 (100 P) and half 2 (200 P ).
It is possible to make every 2 in the following way:
1 × Jun 1 + 1 × 50 p + 2 × 20 p + 1 × 5 p + 1 × 2 p + 3 × 1 p
How many different ways can be used 2 be made using any number of coins?
Import timedef CAL (sum, Count, value): If sum = 200: Count + = 1 returnif sum> 200: returnfor I in range ): sum + = value [I] Cal (sum, Count, value) sum-= value [I] Begin = time. time () num = {100,200,}; num = List (Num) Count = 0cal (0, Count, num) End = time. time () print countprint end-begin
Method 1: Deep Search timeout
# Resolution: retrieve the largest coin value from the list each time, and obtain the upper limit of the maximum number of coins. # overlay the number, and use a smaller number. # Def CAL (value, coins): If value = 0 or Len (coins) = 1: return 1 else: coins = sorted (coins) largest = coins [-1] uses = value/largesttotal = 0for I in range (uses + 1): Total + = CAL (value-largest * I, coins [: -1]) return totalprint CAL (200, [100,200,])
Second, greedy and extreme value AC