码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 357 Let Me Count The Ways

时间:2014-11-06 11:01:41      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:完全背包   动态规划   dp   

跟UVA 674 Coin Change 一样。就是范围变大了而已。

不过当结果只有一种可能的时候需要输出 There is only 1 way to produce n cents change.

int 会溢出,使用 long long


#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<vector>
#include<cmath>

#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,n) for(int i= a;i< n ;i++)
#define FOR0(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define sf scanf
#define pf printf
#define acfun std::ios::sync_with_stdio(false)

#define SIZE 30000 +1
using namespace std;

int main()
{
    LL dp[SIZE];
    CLR(dp,0);
    dp[0]=1;
    int coin[]={1,5,10,25,50};
    FOR(i,0,5)
    {
        FOR(j,coin[i],SIZE)
        dp[j]+=dp[j-coin[i]];
    }

//    FOR(i,0,SIZE)
//    pf("%d %lld\n",i,dp[i]);

    int n;
    while(~sf("%d",&n))
    {
        if(dp[n]==1)
            pf("There is only 1 way to produce %d cents change.\n",n);
        else
            pf("There are %lld ways to produce %d cents change.\n",dp[n],n);
    }
}


UVA 357 Let Me Count The Ways

标签:完全背包   动态规划   dp   

原文地址:http://blog.csdn.net/dongshimou/article/details/40856363

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!