Advertisement
Spocoman

03. Logistics

Sep 8th, 2023
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     const int BUS_CARGO_PRICE_PER_TON = 200;
  7.     const int TRUCK_CARGO_PRICE_PER_TON = 175;
  8.     const int TRAIN_CARGO_PRICE_PER_TON = 120;
  9.  
  10.     int cargoCount;
  11.     cin >> cargoCount;
  12.  
  13.     int busCargo = 0, truckCargo = 0, trainCargo = 0;
  14.  
  15.     int currentCargo;
  16.  
  17.     for (int i = 1; i <= cargoCount; i++) {
  18.         cin >> currentCargo;
  19.  
  20.         if (currentCargo < 4) {
  21.             busCargo += currentCargo;
  22.         }
  23.         else if (currentCargo < 12) {
  24.             truckCargo += currentCargo;
  25.         }
  26.         else {
  27.             trainCargo += currentCargo;
  28.         }
  29.     }
  30.  
  31.     int totalCargo = busCargo + truckCargo + trainCargo;
  32.  
  33.     double average = 1.0 * (busCargo * BUS_CARGO_PRICE_PER_TON + truckCargo * TRUCK_CARGO_PRICE_PER_TON + trainCargo * TRAIN_CARGO_PRICE_PER_TON) / totalCargo;
  34.  
  35.     cout.setf(ios::fixed);
  36.     cout.precision(2);
  37.  
  38.     cout << average << "\n";
  39.     cout << 100.0 * busCargo / totalCargo << "%\n";
  40.     cout << 100.0 * truckCargo / totalCargo << "%\n";
  41.     cout << 100.0 * trainCargo / totalCargo << "%\n";
  42.  
  43.     return 0;
  44. }
  45.  
  46. Решение с тернарен оператор:
  47.  
  48. #include <iostream>
  49.  
  50. using namespace std;
  51.  
  52. int main() {
  53.     const int BUS_CARGO_PRICE_PER_TON = 200;
  54.     const int TRUCK_CARGO_PRICE_PER_TON = 175;
  55.     const int TRAIN_CARGO_PRICE_PER_TON = 120;
  56.  
  57.     int cargoCount;
  58.     cin >> cargoCount;
  59.  
  60.     int busCargo = 0, truckCargo = 0, trainCargo = 0, currentCargo;
  61.  
  62.     for (int i = 1; i <= cargoCount; i++) {
  63.         cin >> currentCargo;
  64.  
  65.         (currentCargo < 4 ? busCargo : currentCargo < 12 ? truckCargo : trainCargo) += currentCargo;    
  66.     }
  67.  
  68.     int totalCargo = busCargo + truckCargo + trainCargo;
  69.  
  70.     double average = 1.0 * (busCargo * BUS_CARGO_PRICE_PER_TON + truckCargo * TRUCK_CARGO_PRICE_PER_TON + trainCargo * TRAIN_CARGO_PRICE_PER_TON) / totalCargo;
  71.  
  72.     cout.setf(ios::fixed);
  73.     cout.precision(2);
  74.  
  75.     cout << average << "\n";
  76.     cout << 100.0 * busCargo / totalCargo << "%\n";
  77.     cout << 100.0 * truckCargo / totalCargo << "%\n";
  78.     cout << 100.0 * trainCargo / totalCargo << "%\n";
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement