HDU-1009-FatMouse' Trade (贪心,sort,qsort)


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.

题目大意(用我逗比的方式 = =):
  

            一共有 i 个房间,揣着 M 个金币的小明路过这些房间的时候,可以选择是否购买这个房间的商品, 假设第一个房间的给的数据是,7 斤豆子两块钱,第二个房间 四斤豆子三块钱,第三个房间五斤豆子两块钱。数据如下:

           5 3     --》 5代表小明又五个金币 ,路过三个房间

           7 2     --》 第一个房间,7 斤豆子 2 块钱,下同
           4 3     --》
           5 2     --》
     

           那么,请用有限的钱买最多的豆子!!!

           简单的贪心问题,妈哒~结果死在 qsort 上面了...太久没用,功能退化了,卡了我半个小时,快哭了 - -

           第一个房间   double per = ( double ) 7 / 2; 算出在这个房间一块钱最多可以买多少豆子。
           使用  ( 可以买到的豆子量 / 一共花费的金币 ) 算出每个房间的比值。用这个比值做一个从大到小的排序 ~ 从比值最大的开始买,就可以买最多啦,像在超市一样滴~

分析第一个样例:
          排序过后数据的排列为:        7     2,        5     2,       4      3
          首先各在房间一与房间二一共花费四个金币,买了 7+5 斤豆子,剩下一个金币,在房间三买了   ( 4/3 ) * 1 斤豆子。


复习一下 qsort 的用法:

int cmp ( const void *_a, const void *_b )

{ 
         int * a =  ( int  *)_a;

         int * b =  ( int * )_b;

         return b - a; // 大 -> 小

}


qsort ( arrary, n, sizeof ( arrary[0] ), cmp);


区别 sort 的用法:

bool sort ( int  a , int b)

{
    return a > b;// 大->小

}

sort ( arrary , arrary + n ,cmp);




贴一个挫挫的代码 :

#include <iostream>
#include <cstring>
#include <algorithm>//sort reverse
#include <stdlib.h>//qsort
#include <cstdio>

using namespace std;
const int MAXN = 1000 + 5;
#define FOR(i,n) for( int i = 0; i < n ; ++i )

typedef struct
{
	double bean, food;
	double per;
}node;


int bcmp ( const void *_a,const void *_b)
{
     node *a = ( node *)_a;
     node *b = ( node *)_b;
     
     if ( a->per > b->per) return -1;
     return 1;

}

node data[MAXN];

bool cmp(node a, node  b)
{
	return a.per > b.per;
}

int main(void)
{
	int n, m;
	while (~scanf("%d%d", &m,&n) && (n != -1) && (m != -1))
	{
		FOR(i, n)
			{
				scanf("%lf %lf", &data[i].bean, &data[i].food);
				data[i].per = (double)data[i].bean / data[i].food;
			}

       //sort(data, data+n, cmp);
       
       qsort(data,n,sizeof(data[0]),bcmp);

       double Bsum = 0.0;

       FOR(i,n)
        {
            if( data[ i ].food < m)
            {
                m -= data[ i ].food;
                Bsum += data[ i ].bean;

            }
            else
            {
                Bsum += (double)m * data[i].per;break;
            }

        }


            printf("%.3lf\n",Bsum);
//		FOR(i, n)
//            printf("%lf %lf %lf\n",data[i].bean,data[i].food,data[i].per);
	}

    return 0;
}








          


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值