IT民工
加油!

这道题和之前的 Coin Change 有点类似,dp[j] = dp[ j - coin[i]];但是得注意数据的范围。

#include<stdio.h>
#include<string.h>
#define MAXD 30005
long long dp[MAXD];

const int coin[] = { 1, 5, 10, 25, 50};

int main()
{
int c;
memset( dp, 0, sizeof dp );
dp[0] = 1;
for( int i = 0; i < 5; i ++)
for( int j = 0; j < 30005; j ++)
if( j - coin[i] >= 0)
dp[j] += dp[j - coin[i]];
while( scanf( "%d", &c) == 1)
{
if( dp[c] > 1)
printf( "There are %lld ways to produce %d cents change.\n", dp[c], c);
else
printf( "There is only %lld way to produce %d cents change.\n", dp[c], c);
}
return 0;
}



 

 

posted on 2011-12-01 10:51  找回失去的  阅读(155)  评论(0编辑  收藏  举报