UVA - 147 - Dollars (集合上的动态规划)

UVA - 147

Dollars

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

Description

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

Source

Root :: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim) :: Chapter 3. Problem Solving Paradigms :: Dynamic Programming :: Coin
Change - Classical

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 5. Dynamic Programming

Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Problem Solving Paradigms :: Dynamic Programming :: Coin
Change (CC)

Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Problem Solving Paradigms :: Dynamic Programming :: Coin
Change (CC)

Submit Status

思路:简单的动态规划问题,因为每个面值都是5cent的整数倍,所以将所有面值都转化为5cent的倍数,即{1, 2, 4, 10, 20, 40, 100, 200, 400, 1000, 2000};

输入的那个数也转化为5的倍数,注意浮点数问题

然后dp预处理下,输出记得域宽要按照要求来,这里dp因为会爆int,要开到LL型

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;

const double eps = 1e-7;
double n;
LL dp[6005];
int val[11] = {1, 2, 4, 10, 20, 40, 100, 200, 400, 1000, 2000};

void init() {
	memset(dp, 0, sizeof(dp));
	dp[0] = 1;
	for(int i = 0; i < 11; i++) {
		for(int j = 0; j < 6005; j++) {
			if(j >= val[i]) dp[j] += dp[j-val[i]];
		}
	}
}

int main() {
	init();
	while(scanf("%lf", &n) != EOF) {
		if( (n - 0) < eps) break;
		int num = (int)( (n + 0.01) / 0.05 );
		printf("%6.2lf%17lld\n", n, dp[num]);
	}
	return 0;
} 
时间: 2024-10-05 20:07:37

UVA - 147 - Dollars (集合上的动态规划)的相关文章

POJ 3181 Dollar Dayz &amp;&amp; Uva 147 Dollars(完全背包)

首先是 Uva 147:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=83 细心看完这题后发现还是完全背包,只不过需要对浮点数处理一下.即把所有硬币的面值都乘以100,化为整数,对输入的数据也作同样的处理,然后就是套完全背包的模板了,在输出时还要用格式和精度来卡一卡你……一开始我没想到用printf可以的,于是百度了cout的输出格式控制,

uva 147 Dollars

题意:给出一个金额问有多少种组成方法: 思路:预处理+递推: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; long long a[500010]; int b[]={1,2,4,10,20,40,100,200,400,1000,2000}; int main() { for(int i=0;i<=6000;i++) a[i]=1; for(int i=1;

UVA 147 Dollars (DP)

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

UVa 103 Stacking Boxes --- DAG上的动态规划

UVa 103 题目大意:给定n个箱子,每个箱子有m个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最多能套几个箱子. 第一行输入为n,m,之后是n行m维的箱子 解题思路:嵌套关系是二元关系,因此这题即在DAG上做动态规划, 只不过将二维的判断改成了n维,其他不变. 详细看考:DAG上的动态规划之嵌套矩形  (ps:这题可以理解成嵌套m边形) /* UVa 103 Stacking Boxes --

ACM:DAG上的动态规划------硬币问题

ExecutorService 建立多线程线程池的步骤: 线程池的作用: 线程池作用就是限制系统中执行线程的数量. 根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果:少了浪费了系统资源,多了造成系统拥挤效率不高.用线程池控制线程数量,其他线程排队等候.一个任务执行完毕,再从队列的中取最前面的任务开始执行.若队列中没有等待进程,线程池的这一资源处于等待.当一个新任务需要运行时,如果线程池中有等待的工作线程,就可以开始运行了:否则进入等待队列. 为什么要用线程池: 1.减少了创建和

DP入门(2)——DAG上的动态规划

有向无环图(DAG,Directed Acyclic Graph)上的动态规划是学习动态规划的基础.很多问题都可以转化为DAG上的最长路.最短路或路径计数问题. 一.DAG模型 [嵌套矩形问题] 问题:有n个矩形,每个矩形可以用两个整数a.b描述,表示它的长和宽.矩形X(a , b)可以嵌套在矩形Y(c , d)中当且仅当a<c,b<d,或者b<c,a<d(相当于把矩形X旋转90°).例如(1,5)可以嵌套在(6, 2)内,但不能嵌套在(3, 4)内.你的任务是选出尽可能多的矩形排

嵌套矩形——DAG上的动态规划

有向无环图(DAG,Directed Acyclic Graph)上的动态规划是学习动态规划的基础.很多问题都可以转化为DAG上的最长路.最短路或路径计数问题. 题目描述: 有n个矩形,每个矩形可以用两个整数a,b描述,表示它的长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b<d,或者b<c,a<d(相当于把矩形X旋转90°).例如(1,5)可以嵌套在(6,2)内,但不能嵌套在(3,4)内.你的任务是选出尽可能多的矩形排成一行.使得除了最后一个之外,每个矩形都

【UVA】10404-Bachet&#39;s Game(动态规划)

如果d[i]是必胜态,那么d[i + V[j]]一定是必败态,反之亦然. 用d[i]代表棋子为i个是否为必胜态. 边界条件是d[i] = 1; 14139291 10404 Bachet's Game Accepted C++ 0.662 2014-09-03 09:44:48 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vecto

禁止字符串 字符串上的动态规划

// 禁止字符串 字符串上的动态规划 // 挑战程序设计第二版 page 368 // 考虑只由'A','G','C','T'四种字符组成的DNF字符串 // 给定一个长度为k的字符串S,计算长度恰好为n的且 // 不包含S的字符串的个数输入结果对10009取膜 // 1<=k<=100 // 1<=n<=10000 // // 这道题想动态规划,肯定是n*k的算法,即10的七次方以内 // 的复杂度 // // 但是,之后就卡住了... // // 仔细研习了书上的思路,发现状态