关闭

完全背包问题UVA147

标签: c语言dfs搜索题目
85人阅读 评论(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网站的观点或立场
    个人资料
    • 访问:756次
    • 积分:120
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档