Sicily.1099. Packing Passengers(优先选择性价比高的飞机)

/* 1099. Packing Passengers(优先选择性价比高的飞机)
   大意:给出两种飞机的价值和最大载客量以及需要载客的总数
         求出A B飞机各需要多少架。
   思路:优先取性价比高的飞机。如B性价比高的话就先从能取得B
         最多的数量开始往下遍历找到合适的即停止 

*/
#include<iostream>
#include<stdlib.h>

using namespace std;
#define INF 1e+15
long long passengerNum, costA, passengersA, costB, passengersB;
bool cal(long long higerCap, long long &higherNum, long long lowerCap, long long &lowerNum){
    if(higerCap == 0)
       if(lowerCap == 0)
          return false;
       else {
          if(passengerNum %lowerCap == 0){
            lowerNum = passengerNum/lowerCap; 
            return true;              
          }
          else            
            return  false;
       }
    else
       if(lowerCap == 0)
          if(passengerNum %higerCap == 0){
            higherNum = passengerNum/higerCap; 
            return true;              
          }
          else            
            return  false;

    higherNum = passengerNum/higerCap;
    long long leftCap = passengerNum - higherNum*higerCap;
    
    while(leftCap % lowerCap!= 0){
          if(higherNum > 0) {
             higherNum--;
          }
          else 
             return false;
          leftCap = passengerNum - higherNum*higerCap;       
    }

    lowerNum = (passengerNum - higerCap*higherNum)/lowerCap;
    return true;
}


int main()
{
    int testNum = 0;
    while(cin >> passengerNum && passengerNum != 0){
         cin >> costA >> passengersA >> costB >> passengersB;
         double priceA = costA == 0 ? INF : (double)passengersA/(double)costA;
         double priceB = costB == 0 ? INF : (double)passengersB/(double)costB; 
         
         long long aNum, bNum;
         bool ok = false;
         if(priceA >= priceB){
            ok = cal(passengersA, aNum, passengersB ,  bNum);          
         }
         else {
            ok =cal(passengersB, bNum, passengersA ,  aNum); 
         }
         testNum++; 
         if(ok){
            cout << "Data set " << testNum << ": "<< aNum << " aircraft A, "<< bNum << " aircraft B" << endl;    
         } 
         else
            cout << "Data set " << testNum << ": cannot be flown" << endl;
    }
    system("pause");
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值