UVA 147 Dp(完全背包)

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

2. 00

0.00

Sample Output

0.20  4

2.00 293

题意:

新西兰的货币由100元、50元、20元、10元和5元的纸币以及2元、1元、50分、20分、10分、和5分硬币组成。要求给定金钱数额,用上述货币组合,计算组合的方法数。

格式:

钱的数量(小数部分两位,向右对齐,宽度为6),方案数(右对齐,宽度为17)

思路:

题目中明确提出输入的数字有效,也就是说我们可以以最小值5分为本题的计算单位。a[i]表示第i种货币含5分硬币的数量(1<=i<=11)

dp[i][j]代表在采用前i种货币构成j个5分硬币的方案数。当i=1时dp[1,j]=1;当2<=i<=11时,状态转移方程为 dp[i][j]=dp[i][j-a[i]]+dp[i-1][j];

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
long long dp[6005];
int a[12]={1,2,4,10,20,40,100,200,400,1000,2000};//各类货币含5分的数量
int main(){
double m;
int n;
for(int i=0;i<=6000;i++)
    dp[i]=1;
for(int i=1;i<11;i++){//枚举种类
    for(int j=a[i];j<=6000;j++)
        dp[j]+=dp[j-a[i]];//累计前1-1类货币构成j-a[i]的方式数
}

while(~scanf("%lf",&m)){
    if(m==0.0)break;
    n=int(m*20.0);
    printf("%6.2f%17lld\n",m,dp[n]);
}
return 0;
}

猜你喜欢

转载自www.cnblogs.com/by-DSL/p/8997377.html