poj 1787 记录路径的多重背包

2024-04-28 17:32

本文主要是介绍poj 1787 记录路径的多重背包,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

 

 

如题:http://poj.org/problem?id=1787

 

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

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.

 

 

题目给出总钱数,要求用1,5,10,25价值的硬币拼出总钱数,并要求硬币尽量用的多。

 

一开始没注意到硬币的要求,将硬币价值作为价值w进行背包,wa了好久

关于记录路径,只要在背包的同时,记录这一层是由那一层过来的就行,并同时记录上一层要多少个硬币才能变为这一层,至于哪一种,就可以用int t=(n-path[n])/count[n];推出。

还有要注意的是背包策略,f[j]并不是在一定金额中尽量多放硬币,f[j]代表的是满足拼成j金额能放硬币的最多数量f[j]=max(f[j-k*c]+k) ,初始化f数组为-inf。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int num[5];
int n;
int w[5]={0,1,5,10,25};
int f[10005];
int path[10005];
int count[10005];


void ZeroOnePack(int k,int c)
{
 int j;
 for(j=n;j>=k*c;j--)
 {
  if(f[j-k*c]+k>f[j])
  {
   f[j]=f[j-k*c]+k;
   path[j]=j-k*c;
   count[j]=k;
  }
 }
}
void CompletePack(int c)
{
 int j;
 for(j=c;j<=n;j++)
 {
  int tv=f[j];
  if(f[j-c]+1>f[j])
  {
   f[j]=f[j-c]+1;
   path[j]=j-c;
   count[j]=1;
  }
  
  
 }
}

void MultiplePack(int c,int cnt)
{
 if(c*cnt>=n)
 {
  CompletePack(c);
  return;
 }
 int k=1;
 while(k<cnt)
 {
  ZeroOnePack(k,c);
  cnt-=k;
  k*=2;
 }
  ZeroOnePack(cnt,c);
}

int main()
{
 //freopen("C:\\1.txt","r",stdin);
 //freopen("C:\\3.txt","w",stdout);
 while(~scanf("%d%d%d%d%d",&n,&num[1],&num[2],&num[3],&num[4])
  &&(n||num[1]||num[2]||num[3]||num[4]))
 {
 // printf("%d %d %d %d %d\n",n,num[1],num[2],num[3],num[4]);
  memset(f,0,sizeof(f));
  memset(path,0,sizeof(path));
  memset(count,0,sizeof(count));
  int i;
  for(i=1;i<=n;i++)
   f[i]=-10005;
  for(i=1;i<=4;i++)
   MultiplePack(w[i],num[i]);
  if(f[n]<=0)
   printf("Charlie cannot buy coffee.\n");
  else
  {
   int c1=0,c2=0,c3=0,c4=0;
   while(n)
   {
    int t=(n-path[n])/count[n];
    if(t==1)
     c1+=count[n];
    else if(t==5)
     c2+=count[n];
    else if(t==10)
     c3+=count[n];
    else
     c4+=count[n];
    n=path[n];
   }
   printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",c1,c2,c3,c4);
  }
 }
 return 0;
}

 

这篇关于poj 1787 记录路径的多重背包的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/943832

相关文章

在Spring Boot中浅尝内存泄漏的实战记录

《在SpringBoot中浅尝内存泄漏的实战记录》本文给大家分享在SpringBoot中浅尝内存泄漏的实战记录,结合实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录使用静态集合持有对象引用,阻止GC回收关键点:可执行代码:验证:1,运行程序(启动时添加JVM参数限制堆大小):2,访问 htt

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

Linux修改pip和conda缓存路径的几种方法

《Linux修改pip和conda缓存路径的几种方法》在Python生态中,pip和conda是两种常见的软件包管理工具,它们在安装、更新和卸载软件包时都会使用缓存来提高效率,适当地修改它们的缓存路径... 目录一、pip 和 conda 的缓存机制1. pip 的缓存机制默认缓存路径2. conda 的缓

Spring Boot中定时任务Cron表达式的终极指南最佳实践记录

《SpringBoot中定时任务Cron表达式的终极指南最佳实践记录》本文详细介绍了SpringBoot中定时任务的实现方法,特别是Cron表达式的使用技巧和高级用法,从基础语法到复杂场景,从快速启... 目录一、Cron表达式基础1.1 Cron表达式结构1.2 核心语法规则二、Spring Boot中定

Windows系统下如何查找JDK的安装路径

《Windows系统下如何查找JDK的安装路径》:本文主要介绍Windows系统下如何查找JDK的安装路径,文中介绍了三种方法,分别是通过命令行检查、使用verbose选项查找jre目录、以及查看... 目录一、确认是否安装了JDK二、查找路径三、另外一种方式如果很久之前安装了JDK,或者在别人的电脑上,想