完全背包问题UVA147

本文介绍了一个用于计算特定金额下不同货币组合数量的程序。该程序考虑了新西兰货币单位,包括纸币和硬币的各种面额,并通过动态规划算法来找出所有可能的组合方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要考试了,实验班一直是我的愿望,虽然现在知道自己估计是上不了
可还是想试试
毕竟努力了那么久
<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
*/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值