• HDU——1009FatMouse' Trade(贪心+结构体+排序)


    FatMouse' Trade

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 60654    Accepted Submission(s): 20406


    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个房间,每个房间前有一只猫,房间里有老鼠最喜欢的食品JavaBean,J[i]。若要引开猫,必须付出相应的猫食F[i]。当然这只老鼠没必要每次都付出所有的F[i]。若它付出F[i]的a%,则得到J[i]的a%。求老鼠能吃到的做多的JavaBean。

    代码:

    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    using namespace std;
    struct food
    {
    	double ji;
    	double fi;
    	double xingjiabi;//按照性价比排序
    };
    bool cmp(const food &a,const food &b)
    {
    	if(a.xingjiabi!=b.xingjiabi)
    		return a.xingjiabi>b.xingjiabi;//性价比高的排前面
    	else
    		return a.fi<b.fi;//性价比一样则需要代价的fi(猫粮)小的排前面,不过再看看我后面的代码似乎不需要这条
    }
    int main(void)
    {
    	int m,n;
    	double ans,rest;
    	while (cin>>m>>n)
    	{
    		if(m==-1&&n==-1)
    			break;
    		food *list=new food[n];
    		for (int i=0; i<n; i++)
    		{
    			scanf("%lf %lf",&list[i].ji,&list[i].fi);
    			list[i].xingjiabi=list[i].ji*1.0/list[i].fi;
    		}	
    		sort(list,list+n,cmp);
    		ans=0;
    		rest=m;
    		for (int i=0; i<n; i++)
    		{
    			if(rest>=list[i].fi)//可以全部拿到直接相加
    			{
    				rest-=list[i].fi;
    				ans+=list[i].ji;
    			}	
    			else
    			{				
    				ans=ans+list[i].xingjiabi*rest;//只能拿到一部分则把剩下的部分全给了,按照付出的百分比进行加成
    				rest-=rest;
    			}
    		}
    		printf("%.3lf
    ",ans);
    		delete []list;
    	}
    	return 0;
    }
  • 相关阅读:
    海龟交易
    暑假攻略:怎样让孩子过一个充实又省钱的假期
    值得追随
    在哪里能找的你想要的答案?
    顺势加仓策略
    交易中 你的加仓策略是怎样的?背后的逻辑是什么?
    驻守深寒:寻找那些有效地关键K线
    统计相关
    求助Ubuntu16.10如何设置默认启动为字符界面
    【Linux系列】Ubuntu ping通,xshell无法连接
  • 原文地址:https://www.cnblogs.com/Blackops/p/5356425.html
Copyright © 2020-2023  润新知