poj 2581 Exact Change Only (dp)



Description

Boudreaux reached over and shook awake Thibodeaux, who had dozed off somewhere in New Mexico. "Where we at?" Thibodeaux groggily yawned.

"Not in Vegas, I gua-ran-tee, but could you get my knapsack?" Boudreaux asked, gesturing to the worn, leather backpack in the back seat of their cherry red Ford Miata.

"Why, is there a problem?"

"Just hand me my knapsack, problem or not."

Thibodeaux complied, glancing up as Boudreaux slowed the car to a stop in a line of vehicles approaching a toll booth. "$1.65 -- Exact change only," Thibodeaux read the yellow sign on the front of a small wooden building occupied by a lone toll booth operator.
"I have to get $1.65 in exact change?" Thibodeaux asked, digging through the knapsack, "all I have are ten quarters, four dimes, and three pennies. I don‘t have any nickels . . ."

"Just give me five of the quarters and the four dimes," Boudreaux replied, holding out his hand.

"Oh yeah," Thibodeaux said, handing over the coins, "that does add up to $1.65. I wish there were an easy way to figure out if you have an exact monetary amount, given a set of coins."

"Hmmm," Boudreaux shrugged, "sounds like a good programming problem."

Input

Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.

A single data set has 1 component:

Start line - A single line:

A B C D E

where:

A: (0.01 ≤ A ≤ 5.00) is a decimal number (to two decimal places) of a monetary amount.

B: (0 ≤ B ≤ 100) is an integer number of quarters (one quarter = $0.25).

C: (0 ≤ C ≤ 100) is an integer number of dimes (one dime = $0.10).

D: (0 ≤ D ≤ 100) is an integer number of nickels (one nickel = $0.05).

E: (0 ≤ E ≤ 100) is an integer number of pennies (one penny = $0.01).

Output

For each data set, there will be exactly one line of output. If there exists one or more subsets of the given coins whose values add up to the given monetary amount exactly, the output will be a single line in the form:

A B C D

where A is the number of quarters, B is the number of dimes, C is the number of nickels, and D is the number of pennies, for the subset with the fewest number of coins. Otherwise, the output will be a single line with the statement:

NO EXACT CHANGE

Sample Input

 0.45 2 1 1 4
0.75 3 7 1 75 

Sample Output

 NO EXACT CHANGE
3 0 0 0 

题意及分析:

好久不写博客了,据学长说,写博客是一个好习惯。哎,,,

这个题可以用for循环直接过,用dp的话,当时听人说要保存路径什么的,,还是胆子小了,不敢自己尝试。应该自己多试试。

AC代码:
时间: 2024-10-17 19:34:25

poj 2581 Exact Change Only (dp)的相关文章

POJ 2581 Exact Change Only(dp)

Language: Default Exact Change Only Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2584   Accepted: 883 Description Boudreaux reached over and shook awake Thibodeaux, who had dozed off somewhere in New Mexico. "Where we at?" Thibod

POJ 3356 AGTC 最短编辑距离 DP

http://poj.org/problem?id=3356 题意: 给两个长度不大于1000的串,修改其中一个串使得两串相同,问最少修改次数.修改有三种,插入一个字符,删除一个字符,改变一个字符. 分析: 直接给方程. dp[i][j]表示第一个串前i位和第二串前j位匹配的最小修改次数. dp[0][0] = 0, dp[length(x)][length(y)]为答案. dp[i][j] = min(dp[i-1][j-1] + x[i] != y[j], dp[i-1][j] + 1, d

【POJ 3034】 Whac-a-Mole(DP)

[POJ 3034] Whac-a-Mole(DP) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3621   Accepted: 1070 Description While visiting a traveling fun fair you suddenly have an urge to break the high score in the Whac-a-Mole game. The goal of the W

POJ Charlie's Change 查理之转换(多重背包,微变形)

题意:给定身上的4种硬币,分别是1 ,5 ,10, 25面额各有多张,要求组成面额p的硬币尽可能多.输出组成p的4种硬币各自的数量. 思路:多重背包,300+ms.用01背包+二进制的方法.记录下所有的硬币的个数以及4种硬币分别的个数,注意初始化dp数组的不同之处. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #define LL long long 5 using namespace

POJ 3356 AGTC (编辑距离 DP)

Description Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given below: Deletion: a letter in x is missing in y at a corresponding position. Insertion: a letter in y is missing in

POJ 1692 Crossed Matchings(DP)

Description There are two rows of positive integer numbers. We can draw one line segment between any two equal numbers, with values r, if one of them is located in the first row and the other one is located in the second row. We call this line segmen

POJ 3666 Making the Grade [DP]

题意: 给定一个序列,以最小代价将其变成单调不增或单调不减序列,这里的代价看题目公式. 思路: 很容易想到是DP. 1. 对前i个序列,构成的最优解其实就是与两个参数有关.一个是这个序列处理后的最大值mx,和这个序列处理的代价值cost. 显然最大值mx最小最好(这样第i+1个值可以不花代价直接接在其后面的可能性更大),cost最小也最好(题意要求),但是两者往往是鱼和熊掌. 用dp[i][j]表示:前i个数构成的序列,这个序列最大值为j,dp[i][j]的值代表相应的cost. 所以状态转移方

POJ 3085 Quick Change (贪心)

Quick Change Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5801   Accepted: 4175 Description J.P. Flathead's Grocery Store hires cheap labor to man the checkout stations. The people he hires (usually high school kids) often make mistak

poj 2081 Recaman&#39;s Sequence (dp)

Recaman's Sequence Time Limit: 3000MS   Memory Limit: 60000K Total Submissions: 22566   Accepted: 9697 Description The Recaman's sequence is defined by a0 = 0 ; for m > 0, am = am−1 − m if the rsulting am is positive and not already in the sequence,