/* Topcoder TCO 2017 Q1B 250-Pointer * James S. Plank * Mon Apr 10 22:27:22 EDT 2017 */ #include #include #include #include using namespace std; class WaterAndOxygen { public: double maxDays(int remainH20, int remainO2, int costH2O, int costO2); }; double WaterAndOxygen::maxDays(int remainH20, int remainO2, int costH2O, int costO2) { double Y, W, X, Z, B, A, Days_W, Days_O; W = remainH20; X = remainO2; Y = costH2O; Z = costO2; /* Do the algebra to calculate A which makes the number of days that you live on water equal the number of days you live on oxygen. */ B = W / Y - X / Z; A = B * ( 2.0 * Z * Y ) / ( Y + 2.0 * Z ); /* Make A legal. */ if (A > W) A = W; if (A < 0) A = 0; /* Now, from A, calculate the number of days you live on water, and the number of days you live on oxygen, and return the minimum. */ Days_W = (W - A) / Y; Days_O = (X + A/2.0) / Z; return (Days_O < Days_W) ? Days_O : Days_W; }