hdu1009简单贪心

FatMouse’ Trade

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

题目大意:就算说给你金额m去n个小仓库里面兑换食物,每个小仓库的价格不同,然后要我们找到让他买食物最多的方法.
思路:贪心,拿样例3说,25 18 可以这么理解我要拿到25样食物,我需要要花费18元,25/18=1.38 就算说我拿一块钱大约可以买个1.38个食物,所以我们当然希望这里比例越大越好,直接做一个降序,然后慢慢买,知道买不了的时候停止,最后买到的就算食物最多的方法.
ac代码(c++):

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<iomanip>
struct t{
	int a;
	int b;
	double rate;
};
struct t tan1[1005];
using namespace std;
bool cmp(struct t a,struct t b){
	return a.rate>b.rate;
}
int main()
{
	int a,b;
	while(cin>>a>>b)
	{
		memset(tan1,0,sizeof(tan1));
		if(a==-1&&b==-1)
		break;
		for(int i=0;i<b;i++){
			cin>>tan1[i].a>>tan1[i].b;
			tan1[i].rate=(double)1.0*tan1[i].a/(double)tan1[i].b;
		}
		sort(tan1,tan1+b,cmp);
		double sum=0;
		for(int i=0;i<b;i++){
			if(a>=tan1[i].b){
				a-=tan1[i].b;
				sum+=tan1[i].a;
			}else{
				sum+=a*tan1[i].rate;
				break;
			}
		}
		cout<<fixed<<setprecision(3)<<sum<<endl; 
	}
}
全部评论

相关推荐

就只能3个月,但是要求长期全职实习
Swaying:你确实是能长期实习啊,但是你那时候有事也没啥办法嘛
点赞 评论 收藏
分享
06-13 12:13
已编辑
东北大学 射频工程师
26毕业的,日常实习还能找到吗
求实习的青提很想去大厂:目前应该还有hc吧,腾讯感觉还有hc,最近捞了我好几次,因为目前有offer,所以不准备面了,可以再找找,不行的话就找找中小厂试试,因为我之前也找了好久,准备放弃了,结果有个岗位流程特别顺利,然后就oc,只能说坚持下试试,万一呢💪
点赞 评论 收藏
分享
每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务