Dollars - UVa 147 dp

来源:互联网 发布:开源图像识别算法 编辑:程序博客网 时间:2024/05/22 17:45

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

思路:首先是dp方程,看题解就好,其次我想说一个精度问题。刚刚试了一下ceil(),floor()和强转(int)的区别。后两者是一样的,ceil是向上取整,floor是向下取整,能够识别是精度是在1*10-16,比如2.99 三者转完是 3 2 2,但2.9999999999999999(共16个9)却是3 3 3。还有就是写法的问题,可以随机应变。

AC代码如下:

#include<cstdio>#include<cstring>#include<cmath>using namespace std;int num[11]={1,2,4,10,20,40,100,200,400,1000,2000};long long dp[6010];int main(){ int n,i,j,k,len1,len2,a,b;  double p;  dp[0]=1;  for(i=0;i<=10;i++)   for(j=num[i];j<=6000;j++)    dp[j]+=dp[j-num[i]];   while(true)   { scanf("%d.%d",&a,&b);     k=(a*100+b)/5;     if(k==0)      break;     if(b>=10)      printf("%3d.%d",a,b);     else      printf("%3d.0%d",a,b);     printf("%17lld\n",dp[k]);   }}



0 0
原创粉丝点击