poj1787 Charlie's Change
Description
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
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
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.
这题可以用多重背包,用dp[m][5]分别记录m价钱刚好能买到的物品个数的最大值,买1元,5元,10元,25元物品的个数。
#include<stdio.h>
#include<string.h>
#define inf 88888888
int max(int a,int b){
return a>b?a:b;
}
int w[10]={0,1,5,10,25},v[10]={1,1,1,1,1,1};
int dp[10006][6];
int main()
{
int n,m,i,j,a,b,c,d,k,ans,sum;
int num[19];
while(scanf("%d%d%d%d%d",&m,&num[1],&num[2],&num[3],&num[4])!=EOF)
{
if(m+num[1]+num[2]+num[3]+num[4]==0)break;
if(m>num[1]+num[2]*5+num[3]*10+num[4]*25){
printf("Charlie cannot buy coffee.\n");continue;
}
if(num[1]>=m){
printf("Throw in %d cents, 0 nickels, 0 dimes, and 0 quarters.\n",m);continue;
}
for(i=0;i<=m;i++){
dp[i][0]=-inf;
}
dp[0][0]=dp[0][1]=dp[0][2]=dp[0][3]=dp[0][4]=0;
for(i=1;i<=4;i++){
ans=num[i]*w[i];
if(ans>=m){
for(j=w[i];j<=m;j++){
if(dp[j-w[i]][0]>=0){
if(dp[j-w[i]][0]+v[i]>dp[j][0]){
dp[j][0]=dp[j-w[i]][0]+v[i];
dp[j][1]=dp[j-w[i]][1];dp[j][2]=dp[j-w[i]][2];dp[j][3]=dp[j-w[i]][3];dp[j][4]=dp[j-w[i]][4];
dp[j][i]++;
}
}
}
}
else{
k=1;sum=0;
while(sum+k<num[i]){
sum+=k;
for(j=m;j>=k*w[i];j--){
if(dp[j-k*w[i]][0]>=0){
if(dp[j-k*w[i]][0]+k*v[i]>dp[j][0]){
dp[j][0]=dp[j-k*w[i]][0]+k*v[i];
dp[j][1]=dp[j-k*w[i]][1];dp[j][2]=dp[j-k*w[i]][2];dp[j][3]=dp[j-k*w[i]][3];dp[j][4]=dp[j-k*w[i]][4];
dp[j][i]+=k;
}
}
}
k=k*2;
}
k=num[i]-sum;
for(j=m;j>=k*w[i];j--){
if(dp[j-k*w[i]][0]>=0){
if(dp[j-k*w[i]][0]+k*v[i]>dp[j][0]){
dp[j][0]=dp[j-k*w[i]][0]+k*v[i];
dp[j][1]=dp[j-k*w[i]][1];dp[j][2]=dp[j-k*w[i]][2];dp[j][3]=dp[j-k*w[i]][3];dp[j][4]=dp[j-k*w[i]][4];
dp[j][i]+=k;
}
}
}
}
}
if(dp[m][0]>0)
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",dp[m][1],dp[m][2],dp[m][3],dp[m][4]);
else printf("Charlie cannot buy coffee.\n");
//printf("%d\n",dp[m][0]);
}
return 0;
}
poj1787 Charlie's Change的更多相关文章
- poj1787 Charlie's Change
思路: 完全背包,记录路径. 实现: #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; ] ...
- (多重背包+记录路径)Charlie's Change (poj 1787)
http://poj.org/problem?id=1787 描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...
- Charlie's Change(完全背包+路径记忆)
Charlie's Change Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3176 Accepted: 913 D ...
- Charlie's Change(完全背包记录路径)
Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffe ...
- Charlie's Change POJ - 1787
Time limit 1000 ms Memory limit 30000 kB description Charlie is a driver of Advanced Cargo Movement, ...
- poj 1787 Charlie's Change (多重背包可作完全背包)
Charlie's Change Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3792 Accepted: 1144 ...
- B - Charlie's Change
Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffe ...
- [POJ 1787]Charlie's Change (动态规划)
题目链接:http://poj.org/problem?id=1787 题意:有4种货币分别是1元,5元,10元,20元.现在告诉你这四种货币分别有多少个,问你正好凑出P元钱最多可以用多少货币.每种货 ...
- POJ 1787 Charlie's Change (完全背包/多重背包,输出方案的物品个数)
网上说是多重背包,因为要输出方案,还要记录下路径,百度一下题解就可以. 自己做的时候,还没了解过多重背包,该题直接往完全背包思考了.咖啡的钱看作总的背包容量,1.5.10.25分别代表四种物品的重量, ...
随机推荐
- o_direct刷新方式和文件系统支持Direct i/o
若让innodb使用o_direct刷新方式,文件系统支持Direct i/o 是非常重要的.为啥
- 【Oracle】用sqlplus登录的各种方式
1.本地登录 sqlplus / as sysdba 2.账号密码登录 sqlplus user/passwd 3.选择实例登录 sqlplus user/passwd@实例名 例如 sqlplu ...
- LeetCode530. 二叉搜索树的最小绝对差
题目 又是常见的BST,要利用BST的性质,即中序遍历是有序递增序列. 法一.中序遍历 1 class Solution { 2 public: 3 vector<int>res; 4 v ...
- [Usaco2008 Mar]Cow Travelling游荡的奶牛
题目描述 奶牛们在被划分成N行M列(2 <= N <= 100; 2 <= M <= 100)的草地上游走,试图找到整块草地中最美味的牧草.Farmer John在某个时刻看见 ...
- java基础-01代理类
简单的代理类实现案例主实现类:ProxyTestimport java.lang.reflect.InvocationHandler;import java.lang.reflect.Proxy;im ...
- kotlin和python哪个好!程序员怎样优雅度过35岁中年危机?满满干货指导
导语 学历永远是横在我们进人大厂的一道门槛,好像无论怎么努力,总能被那些985,211 按在地上摩擦! 不仅要被"他们"看不起,在HR挑选简历,学历这块就直接被刷下去了,连证明自己 ...
- 提取当前文件夹下的所有文件名.bat(Windows批处理文件)
@echo off dir /s/b *.* > 文件名.txt exit
- Fastjson1.2.24反序列化漏洞复现
Fastjson1.2.24 目录 1. 环境简介 1.1 物理环境 1.2 网络环境 1.3 工具 1.4 流程 2. Docker+vulhub+fastjson1.2.24 2.1 Docker ...
- Go 语言编译过程
走进Golang之编译器原理_大愚Talk-CSDN博客 https://blog.csdn.net/hel12he/article/details/103061921 go编译器 - 知乎 http ...
- Go GC: Latency Problem Solved
https://talks.golang.org/2015/go-gc.pdf https://www.oschina.net/translate/go-gc-solving-the-latency- ...