UVA147 完全背包DP(坑爹精度题)

来源:互联网 发布:大非农数据是什么意思 编辑:程序博客网 时间:2024/06/08 06:05

UVA147

这是一道类似UVA674的题,不过这次有50分,20分,10分和5分的硬币了,输入的时候有小数,留意到输入的只有两位小数,所以想直接都乘个100然后完全背包DP就完事了,然后输出的时候格式卡了好久…然后还是各种WA,后来搜了一下题解…居然是精度问题..一口血吐在屏幕上,注意用long long 和 int m = (int)(n*100+0.5)这两个点就好了,想知道大牛是怎么知道加个0.5的…

Description

New Zealand currency consists of 100,50, 20,10, and 5notesand2, $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.20
2.00
0.00

Sample output

0.20 4
2.00 293

代码

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;int coin[] = {5,10,20,50,100,200,500,1000,2000,5000,10000};long long dp[40000];int main(){    dp[0] = 1;    for (int i = 0;i<11;i++)        for(int j = coin[i];j<=30000;j++)         dp[j]+=dp[j-coin[i]];    double n;    while (scanf("%lf",&n)!=EOF)    {        if (n==0)          break;        int m = int(n * 100+0.5);        printf("%6.2f%17lld\n",n,dp[m]);        //printf("%d\n",dp[m]);    }}
0 0
原创粉丝点击