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. Gym100947E || codeforces 559c 组合数取模

    E - Qwerty78 Trip Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  2. 【原】iOS学习之Socket

    Socket在百度百科的定义 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. 相关的描述 Socket的英文原义是“孔”或“插座”.作为BSD UNIX的进 ...

  3. [Java] 位运算

    https://ckjoker.github.io/2015/03/01/Java-bit-primary/

  4. 不注册Activex 直接调用它

    此处的Activex是ATL方式的. [ComVisible(false)]    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnk ...

  5. 我JSP学习心得1

    老师布置了一项作业,说是要按着老师的要求写,但我觉得只要是技术分享的心得就是好的,不论是不是所要求的内容. 由于和几个人在外面给别人搭建网站,项目需要学习了jsp有用到了javascript,这里有一 ...

  6. CF Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)

    1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort    暴力枚举,水 1.题意:n*m的数组, ...

  7. css3动画----实现动态省略号 ...

    <h3>实现省略号点点动,纯css3实现动态省略号</h3>上传中<span class="dot">...</span> [css ...

  8. 指针与数组的区别 —— 《C语言深度剖析》读书心得

    原书很多已经写的很清楚很精炼了,我也无谓做无意义的搬运,仅把一些基础和一些我自己以前容易搞混的地方写一下. 1. 意义: 指针: 指针也是一种类型,长度为4字节,其存放的内容只能是一个地址(4字节). ...

  9. Python os 标准库使用

    os模块是python自带的一个核心模块,用于和操作系统对象进行交互. 1.导入模块获取帮助 >>> import os>>> help(os)>>&g ...

  10. EntityFramework Reverse POCO Code First 生成器

    功能强大的(免费)实体框架工具 Julie Lerman 实体框架是开源的,因此开发社区可以在 entityframework.codeplex.com 上共享代码. 但是不要将自己局限在那里寻找工具 ...