hdu 1009猫鼠取引!!c++

1301 ワード

クリックしてリンクを開く
#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<<:setiosflags>        ,        ,     iomanip   。
     }
 }