Coin change time limit: +Ms | Memory Limit:65535KB Difficulty:3
-
-
Describe
-
In real life, we often encounter coin change problems, for example, in the payroll, the financial personnel need to take into account Count The minimum number of change coins so that they can get the minimum number of coins from the bank and make sure they can pay with these coins. 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, even some of the total amount of money can not be used to change these coins. For example, if the coin system is 40,30,25 yuan, then the PNS Yuan can not use these coins change, 95 yuan minimum change coin number is 3. Another example is that the coin system is 10,7,5,1 Yuan, then 12 dollars in the greedy method to get the number of coins 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.
-
-
Input
-
-
input data:
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
-
-
Output data:
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.
-
-
Sample input
-
-
4 1210 7) 5 1
-
-
Sample output
-
-
2
-
-
The idea of this topic and the rectangle nested a bit similar, the first thing you can think of is to get the least amount of change coins must know every situation, good, so you can use the solution tree to enumerate all the situation, but this obviously will time out, because the situation is too many, and there are many repeated sub-calculations, So since you want to know every situation, use DFS traversal, and then use the idea of memory search to prune, the smallest number of coins has been answered directly back to no longer repeated calculation
-
-
Where Dp[i] represents the decomposition of I into a coin, the minimum can be divided into how many.
-
/*problem:nyoj (Nanyang Polytechnic OJ) author:2486memory:1012 kbtime:192 mslanguage:c/c++result:accepted*/#include <cstdio># Include <cstring> #include <algorithm>using namespace std;const int maxn=100000+5;const int inf=0x3f3f3f3f; int N,t,a[maxn],dp[maxn],min;int dfs (int s) { if (s<0) return INF; if (dp[s]!=-1) return dp[s]; Min=min (s,min); int ans=inf; for (int i=0; i<n; i++) { ans=min (DFS (S-a[i]) +1,ans);//get minimum number of coins } if (Ans!=inf) Dp[s]=ans; return ans;} int main () {while (~scanf ("%d%d", &n,&t), n&&t) {for (int i=0; i<n; i++) { scanf ("%d", &a[i]); } Memset (Dp,-1,sizeof (DP)); dp[0]=0; Min=inf; DFS (t); if (dp[t]==-1) {//Can change dfs (t-min); printf ("%d\n", Dp[t-min]);//If not, repeat recursively } else { printf ("%d\n", Dp[t]);} } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Coin Change-Memory Search (DP dynamic programming)