这道题目是一个完全背包问题,题目中要求数量不超过100 ,所以我给dp数组增加了一个维度用来控制硬币的数量。
这道题目给出了硬币的种类,因此只要先打表,就可以了。
Coin Change
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8112 Accepted Submission(s): 2696
For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or 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 100 coins.
11 26
4 13
#include<iostream>
using namespace std;
int dp[110][350];// [数量][钱数]
int ans[350];
int main()
{
int kk;
int a[]={1,5,10,25,50};
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=0;i<5;i++) //打表
{
for(int j=1;j<=100;j++) //控制硬币的数量
for(int k=a[i];k<300;k++) //换取的面额
{
if(dp[j-1][k-a[i]])
{
dp[j][k]=dp[j][k]+dp[j-1][k-a[i]];
}
}
}
memset(ans,0,sizeof(ans));
for(int i=0;i<300;i++)
{
for(int j=0;j<=100;j++)
ans[i]+=dp[j][i];
}
int aim;
while(cin>>aim)
cout<<ans[aim]<<endl;
return 0;
}
496




被折叠的 条评论
为什么被折叠?



