博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FatMouse' Trade
阅读量:5949 次
发布时间:2019-06-19

本文共 2145 字,大约阅读时间需要 7 分钟。

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. 

InputThe 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. 

OutputFor 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 37 24 35 220 325 1824 1515 10-1 -1

Sample Output

13.33331.500
1 #include
2 #include
3 #include
4 using namespace std; 5 struct kang 6 { 7 double a, b; 8 }; 9 bool cmp(const kang&a, const kang&b)10 {11 return 1.0*a.a / a.b > 1.0*b.a / b.b;12 }13 int main()14 {15 kang k[10000];16 int m, n;17 18 while (cin >> m >> n,n == -1 && m == -1)19 {20 double sum=0;21 for (int i = 0; i < n; i++)22 cin >> k[i].a >> k[i].b;23 sort(k, k + n , cmp);24 for (int i = 0; i < n&&m != 0; i++)25 {26 if (m - k[i].b >= 0)27 {28 sum += k[i].a;29 m -= k[i].b; 30 }31 else32 {33 sum += m * 1.0*k[i].a / k[i].b;34 m = 0;35 }36 }37 printf("%.3f\n", sum);38 }39 return 0;40 }

简单的贪心题,自定义一个cmp就看解决,不过在用sort时注意区间为左闭右开;

也可以使用符号重载:

bool operator < (const kang & a, const kang & b)

{
return a.a / a.b > b.a / b.b;
}(已测试)

 

转载于:https://www.cnblogs.com/kangdong/p/8455870.html

你可能感兴趣的文章
服务器知识详解
查看>>
移位运算陷阱
查看>>
浅谈 Kingshard mysql 中间件
查看>>
TCP/IP详解卷一 学习笔记
查看>>
2003搭建证书服务器
查看>>
mysql连接报错 ERROR 2002 (HY000): Can't connect to local MySQL server through socket
查看>>
我的欧拉工程之路_11
查看>>
js获取URL传参
查看>>
在接受人民币付款后 中国区App Store下载量出现井喷
查看>>
Linux 版lcx
查看>>
java.util.HashMap解读
查看>>
标准滚动条算法
查看>>
字符串练习题
查看>>
Linux的基本指令--服务器
查看>>
在LNMP环境下安装测试HTTPS及其问题
查看>>
二分图最大匹配
查看>>
合并石子,石子合并(环状区间DP)
查看>>
Apache用户认证、域名跳转、Apache访问日志
查看>>
构造函数与拷贝构造函数
查看>>
VM 9 安装64位系统,出现的问题
查看>>