POJ 1787 - Charlie's Change(完全背包+路径记录)
2014-10-20 17:52
471 查看
Description
Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task.
Your program will be given numbers and types of coins Charlie has and the coffee price. The coffee vending machines accept coins of values 1, 5, 10, and 25 cents. The program should output which coins Charlie has to use paying the coffee so that he uses as
many coins as possible. Because Charlie really does not want any change back he wants to pay the price exactly.
Input
Each line of the input contains five integer numbers separated by a single space describing one situation to solve. The first integer on the line P, 1 <= P <= 10 000, is the coffee price in cents. Next four integers, C1, C2, C3,
C4, 0 <= Ci <= 10 000, are the numbers of cents, nickels (5 cents), dimes (10 cents), and quarters (25 cents) in Charlie's valet. The last line of the input contains five zeros and no output should be generated for it.
Output
For each situation, your program should output one line containing the string "Throw in T1 cents, T2 nickels, T3 dimes, and T4 quarters.", where T1, T2, T3, T4 are the numbers of coins of appropriate values Charlie should use to
pay the coffee while using as many coins as possible. In the case Charlie does not possess enough change to pay the price of the coffee exactly, your program should output "Charlie cannot buy coffee.".
Sample Input
Sample Output
题意:
求出使用最多的硬币数得到的钱数的方案。
思路:
可以转化为完全背包问题,使用一个数组used来记录是否超过数量。
CODE:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
const int inf=0xfffffff;
typedef long long ll;
using namespace std;
int a[]={1, 5, 10, 25};
int m[4], dp[10005], used[10005], path[10005], ans[10005];
int main()
{
freopen("in", "r", stdin);
int p;
while(~scanf("%d %d %d %d %d", &p, &m[0], &m[1], &m[2], &m[3])){
if(p == 0 && m[0] == 0 && m[1] == 0 && m[2] == 0 && m[3] == 0) break;
//memset(dp, 0, sizeof(dp));
for(int i = 0; i <= p; ++i) dp[i] = -inf;
memset(path, 0, sizeof(path));
path[0] = -1;
dp[0] = 0;
for(int i = 0;i < 4; ++i){
memset(used, 0, sizeof(used));
for(int j = a[i]; j <= p; ++j){
if(dp[j - a[i]] + 1 > dp[j] && used[j - a[i]] < m[i] && dp[j - a[i]] >= 0){
dp[j] = dp[j - a[i]] + 1;
used[j] = used[j - a[i]] + 1;
path[j] = j - a[i];
}
}
}
memset(ans, 0, sizeof(ans));
if(dp[p] < 0){
printf("Charlie cannot buy coffee.\n");
}
else{
int i = p;
while(1){
if(path[i] == -1) break;
ans[i - path[i]] ++;
i = path[i];
}
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n", ans[a[0]], ans[a[1]], ans[a[2]], ans[a[3]]);
}
}
return 0;
}
Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task.
Your program will be given numbers and types of coins Charlie has and the coffee price. The coffee vending machines accept coins of values 1, 5, 10, and 25 cents. The program should output which coins Charlie has to use paying the coffee so that he uses as
many coins as possible. Because Charlie really does not want any change back he wants to pay the price exactly.
Input
Each line of the input contains five integer numbers separated by a single space describing one situation to solve. The first integer on the line P, 1 <= P <= 10 000, is the coffee price in cents. Next four integers, C1, C2, C3,
C4, 0 <= Ci <= 10 000, are the numbers of cents, nickels (5 cents), dimes (10 cents), and quarters (25 cents) in Charlie's valet. The last line of the input contains five zeros and no output should be generated for it.
Output
For each situation, your program should output one line containing the string "Throw in T1 cents, T2 nickels, T3 dimes, and T4 quarters.", where T1, T2, T3, T4 are the numbers of coins of appropriate values Charlie should use to
pay the coffee while using as many coins as possible. In the case Charlie does not possess enough change to pay the price of the coffee exactly, your program should output "Charlie cannot buy coffee.".
Sample Input
12 5 3 1 2 16 0 0 0 1 0 0 0 0 0
Sample Output
Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters. Charlie cannot buy coffee.
题意:
求出使用最多的硬币数得到的钱数的方案。
思路:
可以转化为完全背包问题,使用一个数组used来记录是否超过数量。
CODE:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
const int inf=0xfffffff;
typedef long long ll;
using namespace std;
int a[]={1, 5, 10, 25};
int m[4], dp[10005], used[10005], path[10005], ans[10005];
int main()
{
freopen("in", "r", stdin);
int p;
while(~scanf("%d %d %d %d %d", &p, &m[0], &m[1], &m[2], &m[3])){
if(p == 0 && m[0] == 0 && m[1] == 0 && m[2] == 0 && m[3] == 0) break;
//memset(dp, 0, sizeof(dp));
for(int i = 0; i <= p; ++i) dp[i] = -inf;
memset(path, 0, sizeof(path));
path[0] = -1;
dp[0] = 0;
for(int i = 0;i < 4; ++i){
memset(used, 0, sizeof(used));
for(int j = a[i]; j <= p; ++j){
if(dp[j - a[i]] + 1 > dp[j] && used[j - a[i]] < m[i] && dp[j - a[i]] >= 0){
dp[j] = dp[j - a[i]] + 1;
used[j] = used[j - a[i]] + 1;
path[j] = j - a[i];
}
}
}
memset(ans, 0, sizeof(ans));
if(dp[p] < 0){
printf("Charlie cannot buy coffee.\n");
}
else{
int i = p;
while(1){
if(path[i] == -1) break;
ans[i - path[i]] ++;
i = path[i];
}
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n", ans[a[0]], ans[a[1]], ans[a[2]], ans[a[3]]);
}
}
return 0;
}
相关文章推荐
- poj 1787 Charlie's Change(完全背包 或 多重背包 记录路径)
- poj 1787 Charlie's Change(多重背包路径记录)
- 【POJ】1787 Charlie's Change(完全背包)
- POJ 1787 Charlie's Change(多重背包+记录路径)
- poj1787 Charlie's Change (多重背包+记录路径)
- 动态规划,多重背包,保存路径,用完全背包的方法做多重背包(Charlie's Change,poj 1787)
- poj 1787 Charlie's Change【多重背包可行性+记录路径】
- POJ 1787 Charlie's Change / 完全背包
- POJ 1787(完全背包+记录路径)
- POJ-1787 Charlie's Change (完全背包+输出方案 入门题)
- POJ 题目1787 Charlie's Change(完全背包)
- POJ-1787 Charlie's Change( 多重背包记录方案)
- POJ - 1787 Charlie's Change(完全背包和路径纪录)
- poj 1787 Charlie's Change(打印路径的多重背包 ->交易数量最大化)
- POJ - 1787 完全背包,记录路径
- poj 1787 多重背包+路径记录
- zoj2156 Charlie's Change 完全背包+路径记录
- poj 1787 记录路径的多重背包
- poj 1787 多重背包 记录路径
- POJ_1787_Charlie'sChange(多重背包&&记录路径)