UVA 147 Dp (complete backpack)

          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

Title:

New Zealand's currency consists of $100, $50, $20, $10 and $5 notes and $2, $1, 50, 20, 10 and 5 cent coins. Ask for a given amount of money, combine the above currencies, and calculate the number of ways to combine them.

Format:

Amount of money (2 decimal places, right-aligned, width 6), number of schemes (right-aligned, width 17)

Ideas:

The question clearly states that the input number is valid, which means that we can divide it into the calculation unit of this question with a minimum value of 5. a[i] represents the number of 5 cent coins in the i-th currency (1<=i<=11)

dp[i][j] represents the number of solutions to form j 5-cent coins in the first i currencies. When i=1, dp[1,j]=1; when 2<=i<=11, the state transition equation is dp[i][j]=dp[i][ja[i]]+dp[i -1][j];

Code:

#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 }; // The amount of 5 cents in various currencies 
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[ja[i]]; // Accumulate the number of ways that the first 1-1 currencies form ja[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;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325313911&siteId=291194637