UVA.357 Let Me Count The Ways (DP 完全背包)

UVA.357 Let Me Count The Ways (DP 完全背包)

题意分析

与UVA.UVA.674 Coin Change是一模一样的题。需要注意的是,此题的数据量较大,dp数组需要使用long long 类型;另外输出方案为1个和多个的时候,语句是不同的。

代码总览

/*
    Title:UVA.357
    Author:pengwill
    Date:2017-2-16
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define nmax 30005
#define ll long long
using namespace std;
ll dp[nmax];
int m[5] = {1,5,10,25,50};
int main()
{
    dp[0] = 1;
    for(int i = 0; i<5 ;++i){
        for(int j= 0;j+m[i]<nmax;++j){
            dp[j+m[i]] += dp[j];
        }
    }
    int n;
    while(scanf("%d",&n)!= EOF){
        if(dp[n] == 1) printf("There is only %lld way to produce %d cents change.\n",dp[n],n);
        else printf("There are %lld ways to produce %d cents change.\n",dp[n],n);
    }
    return 0;
}
posted @ 2017-02-16 00:43  pengwill  阅读(121)  评论(0编辑  收藏  举报