poj 1787 背包+记录路径
http://poj.org/problem?id=1787
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4512 | Accepted: 1425 |
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
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
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.
Source
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define inf 0x3f3f3f3f
int coin[]={,,,,};
int f[];
int book[];
struct date
{
int num,type;
}Q[];
int main()
{
int C[],P,n,m,i,j,k;
while(cin>>P>>C[]>>C[]>>C[]>>C[]){
if(!(P+C[]+C[]+C[]+C[])) break;
memset(f,-inf,sizeof(f));
memset(Q,,sizeof(Q));
memset(book,,sizeof(book));
int W,L,l=;
f[]=;
for(i=;i<=;++i)
{
l=;
for(k=;C[i];C[i]-=k,k*=){
if(C[i]<k) k=C[i];
// cout<<i<<' '<<k<<endl;
W=k*coin[i];
for(j=P;j>=W;--j)
{ if(f[j-W]!=-inf&&f[j]<f[j-W]+k)
{
f[j]=f[j-W]+k;
Q[j].num=k;
Q[j].type=i;
}
}
}
}//puts("dd");
// cout<<f[P]<<endl;
if(f[P]<) puts("Charlie cannot buy coffee.");
else{
j=P;
i=;
while(j){
book[Q[j].type]+=Q[j].num;
j-=coin[Q[j].type]*Q[j].num;
}
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",book[],book[],book[],book[]);
}
}
return ;
}
poj 1787 背包+记录路径的更多相关文章
- 01背包记录路径 (例题 L3-001 凑零钱 (30分))
题意: 就是找出来一个字典序最小的硬币集合,且这个硬币集合里面所有硬币的值的和等于题目中的M 题解: 01背包加一下记录路径,如果1硬币不止一个,那我们也不采用多重背包的方式,把每一个1硬币当成一个独 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- 牛客网暑期ACM多校训练营(第三场) A PACM Team 01背包 记录路径
链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 Eddy was a contestant participating in ACM ICPC ...
- POJ 3414 Pots 记录路径的广搜
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- UVA 624(01背包记录路径)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA624(01背包记录路径)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- - 迷宫问题 POJ - 3984 bfs记录路径并输出最短路径
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- (多重背包+记录路径)Charlie's Change (poj 1787)
http://poj.org/problem?id=1787 描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...
- HDU 6083 度度熊的午饭时光(01背包+记录路径)
http://acm.hdu.edu.cn/showproblem.php?pid=6083 题意: 思路: 01背包+路径记录. 题目有点坑,我一开始逆序枚举菜品,然后一直WA,可能这样的话路径记录 ...
随机推荐
- shader常用
1 模型空间转裁剪空间 UnityObjectToClipPos(v.vertex) 2 模型空间转世界空间 mul( unity_ObjectToWorld, v.vertex ) 3 雾三件套 U ...
- [转载]Css设置table网格线(无重复)
原文地址:Css设置table网格线(无重复)作者:依然贰零零柒 效果图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transition ...
- 《深入理解Linux内核》阅读笔记 --- Chapter 2 Memory Addressing
1.logical address = segment identifier (16bits) + offset (32bits) segment selector其实就是GDT或者LDT的索引,其中 ...
- django博客项目6:Django Admin 后台发布文章
在此之前我们完成了 Django 博客首页视图的编写,我们希望首页展示发布的博客文章列表,但是它却抱怨:暂时还没有发布的文章!如它所言,我们确实还没有发布任何文章,本节我们将使用 Django 自带的 ...
- 服务系统 server端
from django.shortcuts import render,HttpResponse import json # Create your views here. from reposito ...
- 采集baidu搜索信息的java源代码实现(大部分转发,少量自己修改)(使用了htmlunit和Jsoup)(转发:https://blog.csdn.net/zhaohang_1/article/details/44731039)
1.maven依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- linux系统下nginx安装目录和nginx.conf配置文件目录
linux系统下nginx安装目录和nginx.conf配置文件目录 1.查看nginx安装目录 输入命令 # ps -ef | grep nginx 返回结果包含安装目录 root 26 ...
- 如何在浏览器网页中显示word文件内容
如何在浏览器网页中显示word文件内容 把word文件读到byte[]中,再Response.OutputStream.Write(bytes)到客户端去 Page_Load事件中写: //FileS ...
- restful API(转自阮一峰)
RESTful API 设计指南 网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不 ...
- Entity FrameWork Code First常用知识
1.Model属性类: [Key] //标识一个属性作为主键,即使它不符合类名+Id的格式. [MaxLength(500)] //限制一个字符串属性最多有多少字,其对应的数据表字段也会是nvarch ...