uva147 Dollars(完全背包方案)

来源:互联网 发布:剑雨逍遥手游进阶数据 编辑:程序博客网 时间:2024/06/18 14:21

Dollars
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status Practice UVA 147 uDebug
Appoint description: 

Description

Download as PDF

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 tex2html_wrap_inline25 20c, 2 tex2html_wrap_inline25 10c, 10c+2 tex2html_wrap_inline25 5c, and 4 tex2html_wrap_inline25 5c.

Input

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).

Output

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.

Sample input

0.202.000.00

Sample output

  0.20                4  2.00              293


算精度时,要用一个极小数调整,循环时用极大值,用转换后的int值可能会T;


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 30010;
typedef long long LL;
LL k[N], dp[N];


int main()
{
    int v[]={5,10,20,50,100,200,500,1000,2000,5000,10000};
    double m;
    while(scanf("%lf", &m)!=EOF&&m!=0.00)
    {
        memset(dp,0,sizeof(dp));
        int n=(int)(m*100+0.001);
        for(int i=0;i<=n;i++)
        {
            k[i]=1;
        }
        for(int i=0;i<=10;i++)
        {
            for(int j=v[i];j<=N;j++)
            {
                if(dp[j]<dp[j-v[i]]+v[i])
                {
                    k[j]=k[j-v[i]];
                }
                else if(dp[j]==dp[j-v[i]]+v[i])
                {
                    k[j]+=k[j-v[i]];
                }
                dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
            }
        }
        printf("%6.2f%17lld\n",m, k[n]);
    }
    return 0;
}

0 0