zoj 2109 FatMouse' Trade简单的贪心 (注意double数组的排序问题)

2023-11-08 12:08

本文主要是介绍zoj 2109 FatMouse' Trade简单的贪心 (注意double数组的排序问题),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2109

1、问题描述

FatMouse' Trade
Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu
Submit Status Practice ZOJ 2109

Description

FatMouse prepared M pounds of cat food, ready to trade with the cats guarding   the warehouse containing his favorite food, JavaBean.
  The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and   requires F[i] pounds of cat food. FatMouse does not have to trade for all the   JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he   pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning   this homework to you: tell him the maximum amount of JavaBeans he can obtain.


  Input
 
  The input consists of multiple test cases. Each test case begins with a line   containing two non-negative integers M and N. Then N lines follow, each contains   two non-negative integers J[i] and F[i] respectively. The last test case is   followed by two -1's. All integers are not greater than 1000.


  Output
 
  For each test case, print in a single line a real number accurate up to 3 decimal   places, which is the maximum amount of JavaBeans that FatMouse can obtain.


  Sample Input

 
  5 3
  7 2
  4 3
  5 2
  20 3
  25 18
  24 15
  15 10
  -1 -1


  Sample Output
 
  13.333
  31.500


 

2、代码:

#include<stdio.h>
#include<stdlib.h>
struct node
{double j,f;double p;
}a[1010];
int cmp(const void *a,const void *b)
{struct node *c=(node *)a;struct node *d=(node *)b;if(c->p > d->p) return -1;else return 1;
}
int main()
{int N;double M;double ans;while(scanf("%lf%d",&M,&N)){if(M==-1&&N==-1) break;for(int i=0;i<N;i++){scanf("%lf%lf",&a[i].j,&a[i].f);a[i].p=a[i].j/a[i].f;}qsort(a,N,sizeof(a[0]),cmp);ans=0;for(int i=0;i<N;i++){if(M>=a[i].f){ans+=a[i].j;M-=a[i].f;}else{ans+=(a[i].j/a[i].f)*M;break;}}printf("%.3lf\n",ans);}return 0;
}


 

这篇关于zoj 2109 FatMouse' Trade简单的贪心 (注意double数组的排序问题)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

pip install jupyterlab失败的原因问题及探索

《pipinstalljupyterlab失败的原因问题及探索》在学习Yolo模型时,尝试安装JupyterLab但遇到错误,错误提示缺少Rust和Cargo编译环境,因为pywinpty包需要它... 目录背景问题解决方案总结背景最近在学习Yolo模型,然后其中要下载jupyter(有点LSVmu像一个

redis群集简单部署过程

《redis群集简单部署过程》文章介绍了Redis,一个高性能的键值存储系统,其支持多种数据结构和命令,它还讨论了Redis的服务器端架构、数据存储和获取、协议和命令、高可用性方案、缓存机制以及监控和... 目录Redis介绍1. 基本概念2. 服务器端3. 存储和获取数据4. 协议和命令5. 高可用性6.

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

如何解决Pycharm编辑内容时有光标的问题

《如何解决Pycharm编辑内容时有光标的问题》文章介绍了如何在PyCharm中配置VimEmulator插件,包括检查插件是否已安装、下载插件以及安装IdeaVim插件的步骤... 目录Pycharm编辑内容时有光标1.如果Vim Emulator前面有对勾2.www.chinasem.cn如果tools工

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动