hdu 1009 FatMouse' Trade sort结构体

本文提供了一道名为FatMouse'Trade的编程题解析及代码实现。该题旨在通过优化交易策略来最大化收益,适用于算法竞赛训练。代码采用C++编写,实现了数据输入、处理与输出等功能。
    最近杭电的数据很阴险了,

FatMouse' Trade,没过的同学测这组数据:

  1. 0 3 
  2. 100 0 
  3. 100 0 
  4. 100 0 
  5.  
  6. Answer  
  7. 300.000 

  1 #include<iostream>

 2 #include<algorithm>
 3 #include<stdio.h>
 4 using namespace std;
 5 struct  node
 6 {
 7         int j,f;
 8         double a;    
 9 };
10 
11 node test[1005];
12 bool comp(node a1,node a2)
13 {
14      if(a1.a!=a2.a)
15          return a1.a>a2.a;
16      else
17          return 0;
18 }
19 int main()
20 {
21     int m,i,n;
22     
23     double sum;
24     while(scanf("%d%d",&m,&n),m!=-1,n!=-1)
25     {
26        
27           
28       sum=0;
29       for(i=0;i<n;i++)
30       {
31           scanf("%d%d",&test[i].j,&test[i].f);
32           if(test[i].j==0)
33               test[i].a=0;
34           else if(test[i].f==0)
35                test[i].a=static_cast<double>(1001);
36           else
37               test[i].a=double(test[i].j)/test[i].f;
38       }
39       
40       sort(test,test+n,comp);
41       
42       for(i=0;i<n;i++)
43       {
44                 
45                 if(m-test[i].f>=0)
46                 {
47                     sum+=test[i].j;
48                     m-=test[i].f;
49                 }
50                 else
51                 {
52                     sum+=test[i].a*m;
53                     m=0;
54                     
55                 }
56                 
57       }
58       printf("%.3f\n",sum);
59     }
60     return 0;
61 }

转载于:https://www.cnblogs.com/anderson0/archive/2011/05/07/2039777.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值