标签:
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.
For example, if we have 11 cents,
then we can make changes with one 10-cent coin and one 1-cent coin, two
5-cent coins and one 1-cent coin, one 5-cent coin and six 1-cent coins,
or eleven 1-cent coins. So there are four ways of making changes for 11
cents with the above coins. Note that we count that there is one way of
making change for zero cent.
Write a program to find
the total number of different ways of making changes for any amount of
money in cents. Your program should be able to handle up to 7489 cents.
The input file contains any number of lines, each one consisting of a number for the amount of money in cents.
For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.
11 26
4 13
题意:有1,5,10,25,50五种硬币,给出一个数字,问又几种凑钱的方式能凑出这个数。
分析:经典的dp题。。。可以递推也可以记忆化搜索。。。这题题意与HDU2069完全一样,但是相同的代码却提交不了,经常超时,还是看了大神的思路交的。
1 #include <cstdio> 2 #include <cstring> 3 const int MAXN = 8000; 4 const int coin[5] = {1, 5, 10, 25, 50}; 5 int n; 6 long long dp[MAXN][5]; 7 8 long long solve(int i, int s) 9 { 10 if(dp[s][i]!= -1) 11 return dp[s][i]; 12 dp[s][i]=0; 13 for(int j=i;j<5 && s>=coin[j];j++) 14 dp[s][i]+=solve(j,s-coin[j]); 15 return dp[s][i]; 16 } 17 18 int main() 19 { 20 memset(dp,-1,sizeof(dp)); 21 for(int i=0;i<5;i++) 22 dp[0][i]=1; 23 while(~scanf("%d",&n)) 24 printf("%lld\n",solve(0,n)); 25 return 0; 26 }
标签:
原文地址:http://www.cnblogs.com/caterpillarofharvard/p/4244871.html