zoj2156 Charlie's Change 完全背包+路径记录
来源:互联网 发布:手绘头像软件 编辑:程序博客网 时间:2024/05/20 11:32
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 Specification
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 Specification
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.
给定一个数p,要求用四种币值为1,5,10,25的硬币拼成p,并且硬币数要最多,如果无解输出”Charlie cannot buy coffee.”,1<=p<=10000,1<=硬币数量<=10000..直接dp,不过这里要记录路径。。。
#include<cstdio>#include<cmath>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#include<vector>#include<map>#include<stack>#pragma comment(linker,"/STACK:102400000,102400000")#define pi acos(-1.0)#define EPS 1e-6#define INF (1<<24)using namespace std;int main(){ int coin[]={1,5,10,25}; int money; int a[100]; int dp[10005]; int cnt[10005]; int path[10005]; while(scanf("%d %d %d %d %d",&money,&a[0],&a[1],&a[2],&a[3])==5) { if(money==0&&a[0]==0&&a[1]==0&&a[2]==0&&a[3]==0) break; memset(dp,-1,sizeof(dp)); dp[0]=0; int i,j; for(i=0;i<4;i++) { memset(cnt,0,sizeof(cnt)); for(j=coin[i];j<=money;j++) { if(dp[j]<dp[j-coin[i]]+1&&dp[j-coin[i]]!=-1&&cnt[j-coin[i]]<a[i]) { dp[j]=dp[j-coin[i]]+1; path[j]=j-coin[i]; cnt[j]=cnt[j-coin[i]]+1; } } } if(dp[money]==-1) { printf("Charlie cannot buy coffee.\n"); } else { int numbers[50]; memset(numbers,0,sizeof(numbers)); while(dp[money]!=0) { numbers[money-path[money]]++; money=path[money]; } printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",numbers[1],numbers[5],numbers[10],numbers[25]); } } return 0;}
- zoj2156 Charlie's Change 完全背包+路径记录
- POJ 1787 - Charlie's Change(完全背包+路径记录)
- poj 1787 Charlie's Change(完全背包 或 多重背包 记录路径)
- Charlie's Change(完全背包+路径记忆)
- POJ - 1787 Charlie's Change(完全背包和路径纪录)
- POJ 1787 Charlie's Change(多重背包+记录路径)
- POJ1787:Charlie's Change(记录路径的多重背包)
- POJ 1787 Charlie's Change 记录路径的多重背包
- poj1787 Charlie's Change (多重背包+记录路径)
- poj 1787 Charlie's Change【多重背包可行性+记录路径】
- poj1787 Charlie's Change 完全背包
- POJ 1787 Charlie's Change / 完全背包
- Poj 1787 Charlie's Change(多重背包 或者 记录的完全背包)
- zoj 2156 Charlie's Change(多重背包+倍增优化+记录路径)
- 【POJ】1787 Charlie's Change(完全背包)
- POJ 题目1787 Charlie's Change(完全背包)
- poj 1787 Charlie's Change(背包问题输出路径)
- poj1787 Charlie's Change(多重背包+路径)
- [leetcode] 228.Summary Ranges
- iOS应用程序生命周期
- LeetCode题解:Balanced Binary Tree
- 8-26
- BZOJ 2302 HAOI2011 Problem c 动态规划
- zoj2156 Charlie's Change 完全背包+路径记录
- printf函数参数压栈顺序
- String 、 StringBuffer
- IplImage的相关知识
- 关于最大费用最大流的一类问题 【小谈】
- JVM之ClassLoader
- for循环输出<菱形、金字塔>
- C++ 中 dynamic_cast 使用(二)
- hdu-2602-Bone Collector