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

147 - Dollars

Time limit: 3.000 seconds

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

  1. 0.20
  2. 2.00
  3. 0.00

Sample output

  1. 0.20 4
  2. 2.00 293

分析:

这个题跟UVA674题类似,只是这个题有更多的钱的种类,并且需要用long long来保存

做的方法是先乘以100消除浮点数的误差,然后计算,需要注意的是输出格式有要求,有特殊的空格要求~

AC代码:

递推:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. using namespace std;
  5. const int maxn=;
  6. int num[]={,,,,,,,,,,,};
  7. int a,b;
  8. long long f[maxn][];
  9. void Init()
  10. {
  11. for(int i=;i<;i++)
  12. f[][i]=;
  13. for(int i=;i<maxn;i++)
  14. for(int j=;j<;j++)
  15. for(int k=;num[j]*k<=i;k++)
  16. f[i][j]+=f[i-num[j]*k][j-];
  17. }
  18. int main()
  19. {
  20. Init();
  21. while(scanf("%d.%d",&a,&b)&&(a+b))
  22. {
  23. int n=a*+b;
  24. printf("%6.2lf%17lld\n",n*1.0/,f[n][]);
  25. }
  26. return ;
  27. }

背包:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. using namespace std;
  5. const int maxn=;
  6. int num[]={,,,,,,,,,,,};
  7. int a,b;
  8. long long f[maxn][];
  9. void Init()
  10. {
  11. for(int i=;i<;i++)
  12. f[][i]=;
  13. for(int i=;i<maxn;i++)
  14. for(int j=;j<;j++)
  15. for(int k=;num[j]*k<=i;k++)
  16. f[i][j]+=f[i-num[j]*k][j-];
  17. }
  18. int main()
  19. {
  20. Init();
  21. while(scanf("%d.%d",&a,&b)&&(a+b))
  22. {
  23. int n=a*+b;
  24. printf("%6.2lf%17lld\n",n*1.0/,f[n][]);
  25. }
  26. return ;
  27. }

后来想到可以再除以5(题目说是5的倍数),来减少运算量,提高运算效率。

  1. #include <cstdio>
  2. using namespace std;
  3.  
  4. long long f[];
  5. const int num[] = {, , , , , , , , , , };
  6.  
  7. int main()
  8. {
  9. f[] = ;
  10. for(int i = ;i <= ;i ++)
  11. for(int j = num[i] ;j <= ;j ++)
  12. f[j] += f[j - num[i]];
  13. double x;
  14. while(scanf("%lf", &x) , x)
  15. {
  16. double tx = x * ;
  17. printf("%6.2lf%17lld\n", x , f[(int)tx] );
  18. }
  19. return ;
  20. }

uva 147 Dollars的更多相关文章

  1. uva 147 Dollars(完全背包)

    题目连接:147 - Dollars 题目大意:有11种硬币, 现在输入一个金额, 输出有多少种组成方案. 解题思路:uva 674 的升级版,思路完全一样, 只要处理一下数值就可以了. #inclu ...

  2. POJ 3181 Dollar Dayz && Uva 147 Dollars(完全背包)

    首先是 Uva 147:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_p ...

  3. (DP6.1.2.1)UVA 147 Dollars(子集和问题)

    /* * UVA_147.cpp * * Created on: 2013年10月12日 * Author: Administrator */ #include <iostream> #i ...

  4. UVa 147 Dollars(硬币转换)

    题目大意:给出五种硬币,价值分别为 1,5,10,25,50,.当给出一个价值时,求出能够组合的种数(每种硬币可以用无限次). 思路:完全背包, dp[i][j]表示总数 i 能够被表示的种数.状态转 ...

  5. UVa 147 Dollars(完全背包)

    https://vjudge.net/problem/UVA-147 题意: 换零钱,计算方案数. 思路: 完全背包,UVa674的加强版. #include<iostream> #inc ...

  6. UVA 147 Dollars 刀了(完全背包,精度问题)

    题意:一样是求钱的转换方案数,但是这次单位下降到分,但给的是元为单位的,所以是浮点的,但是固定有两位小数. 思路:数据都放大100倍来计算,去除精度问题,转成整型时要注意精度.即使给的是0.02,乘以 ...

  7. hdu 1284 分硬币 && uva 147

    #include<bits/stdc++.h> using namespace std; int main() { unsigned ]; memset(dp,,sizeof(dp)); ...

  8. 专题复习--背包问题+例题(HDU 2602 、POJ 2063、 POJ 1787、 UVA 674 、UVA 147)

    *注 虽然没什么人看我的博客但我还是要认认真真写给自己看 背包问题应用场景给定 n 种物品和一个背包.物品 i 的重量是 w i ,其价值为 v i ,背包的容量为C.应该如何选择装入背包中的物品,使 ...

  9. UVA 147(子集和问题)

     Dollars New Zealand currency consists of $100, $50, $20, $10, and $5 notes and $2, $1, 50c, 20c, 10 ...

随机推荐

  1. Java 根据两个经纬度坐标计算距离

    public class Distance{ private static final double EARTH_RADIUS = 6378137;    private static double  ...

  2. Tomcat 解压版安装

    1.下载tomcat7.0 http://tomcat.apache.org/download-70.cgi

  3. iOS 用xib自定义view控件 debug笔记

    1.在view不是很复杂的情况下,如果多次检查后依旧出现coding-compliant这种情况,干脆彻底删除这个xib重新新建一个xib来做一遍.(至今未明真相) 2.初始化xib中的view的大致 ...

  4. CentOS系统配置记录

    1. 挂載 ntfs: 确定已经安装了rpmforge软件库的源.在线安装使用 yum install 命令 含有 rpmforge源. yum install fuse ntfs-3g -y 安装后 ...

  5. 【贪心】SOJ 13983

    SOJ 13983. Milk Scheduling 这是比赛题,还是作死的我最讨厌的英文题,题目大意就是有n头奶牛,要在喂奶截止时间前给他喂奶并得到相应的含量的牛奶. 一开始的想法就是挑选截止日期的 ...

  6. Qt Visual Studio Add-in 导出的 .pri 怎么用?

    今天咱们介绍一下 Qt Add-in 导出的 pri 文件怎么用.   一般需要导出这个文件, 主要应该是跨平台编译的需求, 所以这个文件里包含的东西会比较少, 咱们看下导出的文件是什么样子的: # ...

  7. MongoDB性能篇之创建索引,组合索引,唯一索引,删除索引和explain执行计划

    这篇文章主要介绍了MongoDB性能篇之创建索引,组合索引,唯一索引,删除索引和explain执行计划的相关资料,需要的朋友可以参考下 一.索引 MongoDB 提供了多样性的索引支持,索引信息被保存 ...

  8. 一些简单编程练习题P【持续更新】

    Q1.写程序将“Hello World”打印到屏幕. A1. public class Test { public static void main(String[] args) { System.o ...

  9. matlab处理图像代码

    1.图像的读取MATLAB中从图像文件中读取数据用函数imread(),这个函数的作用就是将图像文件的数据读入矩阵中,此外还可以用imfinfo()函数查看图像文件的信息(见例1)%例1:图像数据及图 ...

  10. Java-开源工具类

    一.集合 org.springframework.util.CollectionUtils: 二.字符串 com.google.common.base.Strings: org.apache.comm ...