/* 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; }
Sicily.1099. Packing Passengers(优先选择性价比高的飞机)
最新推荐文章于 2019-04-23 14:21:23 发布