FatMouse' Trade

本文介绍了一种通过贪心算法解决猫鼠交易问题的方法。该问题描述了如何使用有限数量的猫粮换取最大数量的JavaBeans。通过定义结构体来表示每间仓库,并使用比较函数对这些仓库进行排序,从而实现最优交换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 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
 

Author
CHEN, Yue

Source
分析:用有的猫粮换取最多的食物,很明显是贪心问题,用结构体做;
代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
struct mouse{
 int pay;
 int get;	
}a[1100];
int cmp(mouse a,mouse b)
{
	return a.get*b.pay>b.get*a.pay;
}
int main()
{
	int n,m,i;
	double num,sum;
	while(~scanf("%d%d",&n,&m))
	{
		if(n==-1&&m==-1)
		break;
		sum=0;num=0;
	 for(i=0;i<m;i++)
	 scanf("%d%d",&a[i].get,&a[i].pay);
	 sort(a,a+m,cmp);
	 for(i=0;i<m;i++)
	 {
	 sum+=a[i].get;
	 num+=a[i].pay;
	 if(num>n)
	{
	 sum-=(num-n)*a[i].get*1.0/a[i].pay;
	 break;
	}
     }
     printf("%.3lf\n",sum*1.0);
	}
	return 0;
}



计算机网络期末复习题 201708-201801计算机网络复习指导发布-校考学生!!下载截止于12.5日晚上9点 2017-12-04 23:07 计算机网络复习指导-校内考试 §1.名词解释: 1. 计算机网络 2.网络体系结构 3.高速以太网 4.静态路由选择 5. 动态路由选择 6.子网掩码 8.动态主机配置协议 9.套接字 10.客户/服务器模型(C/S模型) 11.域名系统(DNS) 12.光纤分布式数据接口FDDI §2.简答题: 1.试谈你对网络安全的威胁和防病毒技术的认识。(线下教学课件) 2.简述物理层的四个特性。(线下教学课件) 3.计算机网络的组成(P2-P3) 4.计算机网络的功能(P3) 5.计算机网络标准化的相关组织(P4+线下教学课件) 6.计算机网络的性能指标(P8+线下教学课件) 7.简述你所知道的网络传输介质(P33-34) 8.物理层的四个特性(P34) 9.简述四种帧边界的划分方法(即组帧方法)。(P52-54+线下教学课件) 10.简述停止-等待流量控制(协议)。(P59+P62-P64+线下教学课件) 11.简述时分多路复用(P69+线下教学课件) 12.简述频分多路复用(P68+线下教学课件) 13.简述IPv4地址的分类(P122-123) 14.UDP数据报与IP分组的区别(P187) §3.论述题: 1.试说明数据链路层原语和协议的转换。(线下教学课件) 2.描述常规释放的过程,并画出其示意图。(线下教学课件) 3.奈奎斯特定理公式和香农定理公式的主要区别是什么? 4.试比较电路交换、报文交换和分组交换等三种数据传输方式。(P27-29) 5.谈谈你对差错控制方法的理解。(P54-58+线下教学课件) 6.谈谈域名解析过程(P225-226) 7.ARP和DNS是否有些相似?它们有何区别?(P227) 8.FTP的工作原理。(P228)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值