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

来源:互联网 发布:压力 知乎 编辑:程序博客网 时间:2024/05/15 03:37

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 longusing 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;}
0 0
原创粉丝点击