完全背包问题。
代码如下:
#include <iostream>
#include <cstdio>
using namespace std;
long long dp[30055];
int coin[5] = {1, 5, 10, 25, 50};
int main()
{
int num;
dp[0] = 1;
for(int i=0; i<5; ++i)
{
for(int j=0; j<30001; ++j)
dp[j + coin[i]] += dp[j];
}
while(scanf("%d", &num) != EOF)
{
if(dp[num] != 1)
printf("There are %lld ways to produce %d cents change.\n", dp[num], num);
else
printf("There is only %lld way to produce %d cents change.\n", dp[num], num);
}
return 0;
}