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

0.20
2.00
0.00

Sample output

  0.20                4
2.00 293

分析:

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

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

AC代码:

递推:

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=;
int num[]={,,,,,,,,,,,};
int a,b;
long long f[maxn][];
void Init()
{
for(int i=;i<;i++)
f[][i]=;
for(int i=;i<maxn;i++)
for(int j=;j<;j++)
for(int k=;num[j]*k<=i;k++)
f[i][j]+=f[i-num[j]*k][j-];
}
int main()
{
Init();
while(scanf("%d.%d",&a,&b)&&(a+b))
{
int n=a*+b;
printf("%6.2lf%17lld\n",n*1.0/,f[n][]);
}
return ;
}

背包:

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=;
int num[]={,,,,,,,,,,,};
int a,b;
long long f[maxn][];
void Init()
{
for(int i=;i<;i++)
f[][i]=;
for(int i=;i<maxn;i++)
for(int j=;j<;j++)
for(int k=;num[j]*k<=i;k++)
f[i][j]+=f[i-num[j]*k][j-];
}
int main()
{
Init();
while(scanf("%d.%d",&a,&b)&&(a+b))
{
int n=a*+b;
printf("%6.2lf%17lld\n",n*1.0/,f[n][]);
}
return ;
}

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

 #include <cstdio>
using namespace std; long long f[];
const int num[] = {, , , , , , , , , , }; int main()
{
f[] = ;
for(int i = ;i <= ;i ++)
for(int j = num[i] ;j <= ;j ++)
f[j] += f[j - num[i]];
double x;
while(scanf("%lf", &x) , x)
{
double tx = x * ;
printf("%6.2lf%17lld\n", x , f[(int)tx] );
}
return ;
}

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. (iOS)项目总结-项目中遇到的各种的问题和解决方法

    前言: 一到公司报道那时,便着手独立的去完成了一个项目,其中的辛酸泪也是不足为外人道也.这次算是一个新型的app,仍然是独立开发,但心境和想法却是完全的不同.下面说一次以前做开发时常常忽略的知识,也算 ...

  2. DirectShow

    1 最简单的DirectShow应用程序 — 播放视频 1.简介DirectShow是DirectX中的一套处理媒体播放.音视频采集的开发包,在DirectX SDK Summer 2004(Dire ...

  3. CSS:文字不在图片中间

    平时用的text-align属性比较多,相比较而言vertical-align则用的比较少. 当文字和图片布局在一起时,文字不能对齐到图片的中间,向这样: HTML: <div><i ...

  4. ZeroMQ接口函数之 :zmq_ctx_get - 得到环境上下文的属性

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_ctx_get zmq_ctx_get(3)  ØMQ Manual - ØMQ/3.2.5 Name zmq_c ...

  5. 【noip 2004】 合并果子

    noip2016结束后的第一份代码--优先队列的练习 合并果子 原题在这里 #include <iostream> #include <queue> #include < ...

  6. 试验删除RAC(ORA10G)节点

    1.环境概述 2.删除一个节点 本实验中以删除第二个节点为示例. 2.1删除数据库实例 在第一个节点上(存活的节点)上使用DBCA删除第二个节点的实例. 执行完以上操作可以看下crs_stat –t  ...

  7. linux常用命令(四)

    1.压缩解压命令 gzip命令 默认为.gz格式文件 1.只能压缩文件不可压缩目录 2.不保留源文件 压缩   giz 解压   gunip tar命令 -c产生打包文件 -v显示相信打包压缩过程 - ...

  8. Yii源码阅读笔记(三十四)

    Instance类, 表示依赖注入容器或服务定位器中对某一个对象的引用 namespace yii\di; use Yii; use yii\base\InvalidConfigException; ...

  9. ExtJS笔记 Ext.data.Types

    This is a static class containing the system-supplied data types which may be given to a Field. Type ...

  10. mysql5.1升级到mysql5.6

    这么大跨度的升级,本身不推荐.升级应该是循序渐进的,以防止不兼容.如果确实要这么做,你先要确保你的客户端管理工具要能兼容,比如phpmyadmin,此次升级依然存在一个问题,mysql server和 ...