关闭

完全背包问题UVA147

标签: c语言dfs搜索题目
179人阅读 评论(0) 收藏 举报
分类:
要考试了,实验班一直是我的愿望,虽然现在知道自己估计是上不了
可还是想试试
毕竟努力了那么久
<table bgcolor="#0060f0"><tbody><tr><td><strong><span style="font-size:24px; color:#c0ffff"><a target=_blank name="SECTION0001000000000000000000" target="_blank">Dollars</a></span> </strong></td></tr></tbody></table><p>New Zealand currency consists of $100, $50, $20, $10, and $5 notes and $2, $1, 50c, 20c, 10c and 5c coins. Write a program that will determine, for any given amount, in how many ways that amount may be made up. Changing the order of listing does not increase the count. Thus 20c may be made up in 4 ways: 1 <img width="9" height="18" align="MIDDLE" alt="tex2html_wrap_inline25" src="http://uva.onlinejudge.org/external/1/147img1.gif" /> 20c, 2 <img width="9" height="18" align="MIDDLE" alt="tex2html_wrap_inline25" src="http://uva.onlinejudge.org/external/1/147img1.gif" /> 10c, 10c+2 <img width="9" height="18" align="MIDDLE" alt="tex2html_wrap_inline25" src="http://uva.onlinejudge.org/external/1/147img1.gif" /> 5c, and 4 <img width="9" height="18" align="MIDDLE" alt="tex2html_wrap_inline25" src="http://uva.onlinejudge.org/external/1/147img1.gif" /> 5c.</p><p></p><h2><a target=_blank name="t0"></a><span style="color:#0070e8"><a target=_blank name="SECTION0001001000000000000000" target="_blank">Input</a></span></h2><p></p><p></p><p>Input will consist of a series of real numbers no greater than $300.00 each on a separate line. Each amount will be valid, that is will be a multiple of 5c. The file will be terminated by a line containing zero (0.00).</p><p></p><h2><a target=_blank name="t1"></a><span style="color:#0070e8"><a target=_blank name="SECTION0001002000000000000000" target="_blank">Output</a></span></h2><p></p><p></p><p>Output will consist of a line for each of the amounts in the input, each line consisting of the amount of money (with two decimal places and right justified in a field of width 6), followed by the number of ways in which that amount may be made up, right justified in a field of width 17.</p><p></p><h2><a target=_blank name="t2"></a><span style="color:#0070e8"><a target=_blank name="SECTION0001003000000000000000" target="_blank">Sample input</a></span></h2><p></p><p></p><p></p><pre>0.20
2.00
0.00

Sample output

  0.20                4
  2.00              293

#include<iostream>
#include<memory.h>
#include<stdio.h>
#include<string.h>
#define ll long long int
ll b[101],dp[11][10000];
int n;
int mon[11]={5,10,20,50,100,200,500,1000,2000,5000,10000};
int i;
float a[101];

using namespace std;
/*
ll DP(int  x,ll y)
{
        int m,k,h;
        if(dp[x][y]!=-1)
                return dp[x][y];
        dp[x][y]=0;
                for(m=0;y-m*mon[x]>=0;m++)
        {
                dp[x][y]+=DP(x-1,y-m*mon[x]);


        }
        return dp[x][y];
}
*/
ll DP(int  x,ll y)
{
        int m,k,h;
        if(dp[x][y]!=-1)
                return dp[x][y];
        dp[x][y]=0;
                for(m=0;y-m*mon[x]>=0;m++)
        {
                dp[x][y]+=DP(x-1,y-m*mon[x]);


        }
        return dp[x][y];
}
int main()
{
        dp[0][0]=0;
        int k=0;
        cin>>a[k];
        while(1)
        {

                memset(dp,-1,sizeof(dp));
                n=(int )(a[k]*100+0.5);
                //cout<<n;
                if(n==0)
                        break;
                        for(i=0;i<=n;i++)
                        {
                                dp[0][i]=1;
                        }
                       // cout<<a[k];
                b[k]=DP(10,n);
                k++;
        cin>>a[k];

        }
        for(i=0;i<k;i++)
        {
                printf("%.2f %d",a[i],b[i]);
        }
}

/*
#include<stdio.h>
#define N 30005
#include<string.h>
#define ll long long
int d[11]= {5,10,20,50,100,200,500,1000,2000,5000,10000};
ll dp[15][N];//dp[i][j]表示用前i+1种硬币,组成j分的种类数
ll DP(ll i,ll j)
{
    //printf("%d %d %d\n",i,j,dp[i][j]);
    if(dp[i][j]!=-1)
        return dp[i][j];
    dp[i][j]=0;
    for(int k=0;j-k*d[i]>=0;k++)
    {
        dp[i][j]+=DP(i-1,j-k*d[i]);
    }
    return dp[i][j];
}
int main()
{
    double nn;
    memset(dp,-1,sizeof(dp));//赋值一次即可,否则可能会超时
    while(scanf("%lf",&nn)!=EOF)
    {
        if(nn==0.00)
            break;
        int n=(int)(nn*100+0.5);//注意精度
        for(ll i=0; i<=n; i++)
            dp[0][i]=1;
            printf("%6.2f%17lld\n",nn,DP(10,n));
    }
    return 0;
}
/*
0.20
2.00
0.00
  0.20                4
  2.00              293
*/




0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

背包问题详解:01背包、完全背包、多重背包

参考链接: http://www.cnblogs.com/fengty90/p/3768845.html http://blog.csdn.net/mu399/article/details/7722...
  • na_beginning
  • na_beginning
  • 2017-03-17 11:47
  • 2024

01背包问题和完全背包问题

在hihocoder上面两期的题目,一个01背包问题,一个完全背包问题。总结一下!
  • KangRoger
  • KangRoger
  • 2014-08-27 10:27
  • 50928

经典背包问题 01背包+完全背包+多重背包

01 背包 有n 种不同的物品,每个物品有两个属性,size 体积,value 价值,现在给一个容量为 w 的背包,问最多可带走多少价值的物品。           int f[w+1];   //...
  • LYHVOYAGE
  • LYHVOYAGE
  • 2013-04-06 20:27
  • 23953

UVa 147 Dollars(DP完全背包)

Dollars  New Zealand currency consists of $100, $50, $20, $10, and $5 notes and $2, $1, 5...
  • jd503058037
  • jd503058037
  • 2014-07-09 21:39
  • 448

UVA - 147 Dollars (完全背包 入门题)

完全背包的入门题。有11种硬币,多组cases,每组case给出一个总价格n,问n可以有多少种方式通过这些硬币组合起来。
  • hmc0411
  • hmc0411
  • 2017-10-30 22:51
  • 265

UVA 147 Dollars (完全背包)

UVA 147 Dollars (完全背包) http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=sh...
  • u013480600
  • u013480600
  • 2014-10-23 21:19
  • 729

完全背包问题的三种算法的java实现

  • 2016-10-21 16:01
  • 7KB
  • 下载

背包问题详解(01背包,完全背包,多重背包,混合背包,二维费用背包……)

  • 2010-10-16 22:21
  • 124KB
  • 下载

背包问题(0-1背包,完全背包,多重背包知识概念详解)

  • 2015-06-14 13:05
  • 186KB
  • 下载

hdu 1114 Piggy-Bank 动态规划 之完全背包问题

昨天听了羽哥的讲课,发现自己在dp问题上就是个小白~~ 切两道完全背包的题目来练习一下  继续学习背包九讲中 Piggy-Bank Time Limit: 2000/1000 MS (J...
  • yangshuolll
  • yangshuolll
  • 2012-12-15 22:58
  • 1281
    个人资料
    • 访问:1313次
    • 积分:127
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档