hdu 1009 猫鼠交易!!c++

点击打开链接

#include
#include
#include
#include
using namespace std;

 struct Trade
 {
     int food;
     int javabean ;
     double rate ;
 } mouse[1002] ;

 bool cmp(Trade a , Trade b)
 {
     return a.rate > b.rate ;
 }

 int main()
 {
     int Catfood , room , i ;

     while(cin >> Catfood >> room && !(Catfood == -1 || room == -1))
     {
         for(i = 0 ;i < room; i++)
         {
             cin >> mouse[i].javabean >> mouse[i].food ;
             mouse[i].rate = mouse[i].javabean*1.0/mouse[i].food ;
         }
         sort(mouse , mouse+room , cmp) ;
         double sum = 0;
         for(i = 0 ;i < room ; i++)
         {
             if(Catfood > mouse[i].food)
             {
                 sum += mouse[i].javabean ;
                 Catfood -= mouse[i].food ;
             }
             else
             {
                 sum += mouse[i].rate*Catfood ;
                 break ;
             }
         }
         cout << setiosflags(ios::fixed) << setprecision(3) << sum << endl ;
         //std::cout< 头文件必须包含,这个没什么好说的,上面的都在iomanip中定义。
     }
 }



 

你可能感兴趣的:(贪心)