当前位置: 代码迷 >> 综合 >> uva 147 - Dollars(动态规划--完全背包)
  详细解决方案

uva 147 - Dollars(动态规划--完全背包)

热度:18   发布时间:2024-01-13 20:55:18.0

1、http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=83

2、题目大意:

给定11种面值分别为$100, $50, $20, $10, and $5 notes and $2, $1, 50c, 20c, 10c and 5c coins的钱,现在给定一个钱数,求出可以组成的种类数,类似于uva 674,不过此题的精度太强了,纠结。。。

int n=(int)(nn*100+0.5);,注意用long long ,种类数可能非常大

用dp[i][j]表示用前i种硬币,组成j分的种类数,

状态转移方程:dp[i][j]+=DP(i-1,j-k*d[i]);

 

3、题目:

 

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 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                42.00              293

 

4、AC代码:

#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种硬币,组成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.000.20                42.00              293
*/