Title Source: NYOJ995
Problem Description:
In real life, we often encounter coin change problems, for example, in the payroll, the financial staff need to calculate the minimum number of change coins, so that they can get the minimum number of coins from the bank, and ensure that they can use the coins to pay.
We should note that the renminbi coin system is 100,50,20,10,5,2,1,0.5,0.2,0.1,0.05,0.02,0.01 yuan, using these coins we can use the greedy algorithm to find out the minimum number of coins for any one wage number. But unfortunately: we may not have such a good coin system, so the greedy algorithm can not find the minimum number of coins, and even some of the sum of money can not use these coins change. For example, if the coin system is 40,30,25 yuan, then $37 will not be able to use these coins for change, and a minimum of 95 coins is 3. Another example is that the coin system is 10,7,5,1 yuan, then the number of coins obtained by the greedy method of $12 is 3, and the minimum number of coins is 2. Your task is: for any coin system and a sum of money, please program to find the minimum number of change coins, if you can not use these coins change, please give a change method, so that the remaining money the least. (Many topics do not need to be considered in this case)
Input:
Multiple sets of test data, each set as follows:
Line 1th, N and T, where 1≤n≤50 is the number of different coins in the coin system; 1≤t≤100000 is the total number of coins needed for change.
The 2nd behavior is a positive integer with a value not greater than 65535, which is the face value of each coin in the coin system.
When the n,t ends at 0 o'clock.
Output:
If T can be replaced by coins in the coin system, please output a minimum number of change coins.
If T cannot be replaced by coins in the coin system, please output the minimum number of coins in the change scheme with the least amount of money left.
Analysis:
Assuming that the value of n coins is sorted in ascending order, then V1, v2, ... vn, the minimum number of coins required for the total change of change to T is indicated by F (t), and when f (t) = 0 o'clock, the T is not combined with these coins.
For the total change T, it can be decomposed into t = (T-VI) + VI, where i = 1 ... n. Therefore, you can get a recursive relationship :
F (T) = min (f (t-vi)) + 1, where i = 1 ... n)
Initial conditions :
F (vi) = 1 (where I =1 ... n),
When T < V1, f (t) = 0;
Code:
1, recursion: (from top to bottom, from big to small)
By the above recursive relationship and initial conditions, you can simply write a recursive program to solve the problem. There is a large number of repeated computations in the recursive process. Therefore, you can set a global tag array as a memo to avoid duplicate calculations.
However, due to the title "If you can not use these coins change, give a change method, so that the rest of the least money", which makes use of recursive method, if f (T) = 0 o'clock, need to continue to calculate F (T-1), F (T-2) ... until the F (t-i)!=0.
2, recursion: (from the bottom up, from small to large)
By the recursive relationship: F (t) = min (f (t-vi)) + 1, where T-vi < T is constant, and thus can be guaranteed from small to recursive, when calculating f (t), the value of f (T-VI) has been obtained.
Because the value of f (t) needs to have F (t-vi), the recursive method requires the array to record F (1) ... the value of f (t), so that when f (t) = 0 o'clock, you can traverse the array from F (t) down and find the first F (t)!=0.
In the recursive process, it is also possible to get an array record of how each T is combined to make the minimum number of coins required.
Recursive code See GitHub:
Algorithmic note-The minimum number of coins to change coins