uva 147 Dollars
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的更多相关文章
- uva 147 Dollars(完全背包)
题目连接:147 - Dollars 题目大意:有11种硬币, 现在输入一个金额, 输出有多少种组成方案. 解题思路:uva 674 的升级版,思路完全一样, 只要处理一下数值就可以了. #inclu ...
- POJ 3181 Dollar Dayz && Uva 147 Dollars(完全背包)
首先是 Uva 147:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_p ...
- (DP6.1.2.1)UVA 147 Dollars(子集和问题)
/* * UVA_147.cpp * * Created on: 2013年10月12日 * Author: Administrator */ #include <iostream> #i ...
- UVa 147 Dollars(硬币转换)
题目大意:给出五种硬币,价值分别为 1,5,10,25,50,.当给出一个价值时,求出能够组合的种数(每种硬币可以用无限次). 思路:完全背包, dp[i][j]表示总数 i 能够被表示的种数.状态转 ...
- UVa 147 Dollars(完全背包)
https://vjudge.net/problem/UVA-147 题意: 换零钱,计算方案数. 思路: 完全背包,UVa674的加强版. #include<iostream> #inc ...
- UVA 147 Dollars 刀了(完全背包,精度问题)
题意:一样是求钱的转换方案数,但是这次单位下降到分,但给的是元为单位的,所以是浮点的,但是固定有两位小数. 思路:数据都放大100倍来计算,去除精度问题,转成整型时要注意精度.即使给的是0.02,乘以 ...
- hdu 1284 分硬币 && uva 147
#include<bits/stdc++.h> using namespace std; int main() { unsigned ]; memset(dp,,sizeof(dp)); ...
- 专题复习--背包问题+例题(HDU 2602 、POJ 2063、 POJ 1787、 UVA 674 、UVA 147)
*注 虽然没什么人看我的博客但我还是要认认真真写给自己看 背包问题应用场景给定 n 种物品和一个背包.物品 i 的重量是 w i ,其价值为 v i ,背包的容量为C.应该如何选择装入背包中的物品,使 ...
- UVA 147(子集和问题)
Dollars New Zealand currency consists of $100, $50, $20, $10, and $5 notes and $2, $1, 50c, 20c, 10 ...
随机推荐
- JS:XML
一 IE中的XML //1.创建XMLDOM对象 //创建XMLDOM对象 var xmlDom = new ActiveXObject("MSXML2.DOMDocument.6.0&qu ...
- ZeroMQ接口函数之 :zmq_plain - 明文认证
ZeroMQ 官方地址 :http://api.zeromq.org/4-2:zmq_plain zmq_plain(7) ØMQ Manual - ØMQ/4.1.0 Name zmq_plain ...
- 基于socket的TCP和UDP编程
一.概述 TCP(传输控制协议)和UDP(用户数据报协议是网络体系结构TCP/IP模型中传输层一层中的两个不同的通信协议. TCP:传输控制协议,一种面向连接的协议,给用户进程提供可靠的全双工的字节流 ...
- 从DataGridView导出Excel
从DataGridView导出Excel的两种情况,不多说,直接记录代码(新建类,直接引用传入参数). using System; using System.Collections.Generic; ...
- JS问题汇总
1.Q:$(this)在js中失效,无法获取当前元素 A:function()在被调用时this是指向window的,如果要想指向被点击的元素,一般是将this作为参数传入,例如: <div ...
- 【最简单IOC容器实现】实现一个最简单的IOC容器
前面DebugLZQ的两篇博文: 浅谈IOC--说清楚IOC是什么 IoC Container Benchmark - Performance comparison 在浅谈IOC--说清楚IOC是什么 ...
- O(n) 筛法求素数
var tot,i,j,k,m,n:longint; prime:array[0..100000] of boolean; p:array[0..100000] of longint;begin re ...
- 使用非Web方式从CA申请证书
背景介绍:关于从CA申请证书这点事,网上的那些教程基本都是让我们通过访问https://server/certsrv这样的网页来操作的,我一直希望不依赖IIS就把这事干了,于是就有了下面的文章. 1. ...
- php ajax请求和返回
define('NOW_TIME', $_SERVER['REQUEST_TIME']); define('REQUEST_METHOD',$_SERVER['REQUEST_METHOD']); d ...
- [不好分类]SD卡无法读取,显示RAW
上周同事拿来了一个8G的SD卡,插入读卡器后显示“需要格式化”.无法读取.文件格式处显示“RAW”,磁盘大小显示0字节. 处理步骤如下: 1.按照提示,格式化,选择“快速格式化”. 2.采用数据恢复软 ...