【解题报告】uva147_Dollars(美元, dp, 完全背包)

来源:互联网 发布:淘宝店被降权怎么办 编辑:程序博客网 时间:2024/06/06 17:51

147 - Dollars

Time limit: 3.000 seconds

Dollars 

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_inline2510c, 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


题目大意:

给出11种面值的硬币($100, $50, $20, $10, and $5 notes and $2, $1, 50c, 20c, 10c and 5c coins),每次输入一个金额,以元为单位,求用所给硬币组合出该金额共有多少种组合方案,硬币的顺序不影响方案数量。


解题思路:

将所有面值的硬币与输入金额转化为统一的度量单位——分,即可看成完全背包问题。

定义状态d(i,j),代表用前i种硬币组合出金额j的方案数。

状态转移方程:d(i,j) = ∑ { d(i-1,j-k*c[i]) | 1<=k<=j/c[i], c[i]<=j }


#include <cstdio>#include <cstring>int c[11]={5,10,20,50,100,200,500,1000,2000,5000,10000};//硬币面额long long d[30010];int main(){    d[0]=1;//init    for(int i=0;i<11;++i){//枚举11种硬币,        for(int j=c[i];j<=30000;++j){//升序枚举所有金额状态            d[j]+=d[j-c[i]];        }    }    double a;    while(~scanf("%lf",&a),a){        int A=int(a*100+0.5);//扩大100倍,单位转换为分,注意要四舍五入        printf("%6.2lf%17lld\n",a,d[A]);    }    return 0;}


0 0