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. 我的c++学习(10)this指针

    问题:当在对象的外部访问该对象的公有成员时,必须指明是哪一个对象.但是当我们用对象的成员函数来访问本对象的成员时,在成员函数中只要给出成员名就可以实现对该对象成员的访问.再进一步可用同一个类创建很多个 ...

  2. Java动态、重写 理解

    相关类: class A ...{ public String show(D obj)...{ return ("A and D"); } public String show(A ...

  3. linux系统meminfo详解(待补充)

    ========================================================================================== MemTotal: ...

  4. [ACM训练] 数据结构----树、二叉树----c++ && python

    树结构,尤其是二叉树结构是算法中常遇见的,这里根据学习过程做一个总结. 二叉树所涉及到的知识点有:满二叉树与完全二叉树.节点数目的关系.节点数与二叉树高度的关系.层次遍历.深度优先遍历.广度优先遍历等 ...

  5. javascript创建跟随鼠标好玩的东西

    不说话,直接上代码. css: #createGoDivBox{ display: none; } #createGoDivBox div{ background-color: #00A6C2; po ...

  6. 【转】最近公共祖先(LCA)

    基本概念 LCA:树上的最近公共祖先,对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. RMQ:区间最小值查询问题.对于长度为n的 ...

  7. 2016huasacm暑假集训训练三 F - Jungle Roads

    题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/F 题意:在相通n个岛屿的所有桥都坏了,要重修,重修每一个桥所用的时间不同,求重修使 ...

  8. 20145205 《Java程序设计》第8周学习总结

    教材学习内容总结 第十五章 通用API 15.1 日志 日志API简介 java.util.logging包提供了日志功能相关类与接口,不必额外配置日志组件,就可在标准Java平台使用是其好处.使用日 ...

  9. awk匹配

    输出匹配funcno或type:awk 'funcno|type' 输出两次正则表达式匹配之间的行:awk '/funcno/, /type/' 删除所有的空行:awk NF 从第8行输出到第12行: ...

  10. 利用navicat创建存储过程、触发器和使用游标的简单实例

    利用navicat创建存储过程.触发器和使用游标的简单实例 标签: navicat存储过程触发器mysql游标 2013-08-03 21:34 15516人阅读 评论(1) 收藏 举报  分类: 数 ...