Advertisement
Spocoman

01. Dishwasher

Sep 11th, 2023
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     const int BOTTLED_DETERGENT_IN_MILILITERS = 750;
  8.  
  9.     int detergentCount, counter = 1, dishes = 0, pots = 0;
  10.     cin >> detergentCount;
  11.  
  12.     int detergentQuantity = detergentCount * BOTTLED_DETERGENT_IN_MILILITERS;
  13.  
  14.     string command;
  15.  
  16.     while (true) {
  17.  
  18.         cin >> command;
  19.  
  20.         if (command == "End") {
  21.             cout << "Detergent was enough!\n"
  22.                 << dishes << " dishes and " << pots << " pots were washed.\n"
  23.                 << "Leftover detergent " << detergentQuantity << " ml.\n";
  24.             break;
  25.         }
  26.  
  27.         int detergentRefillInMililiters = stoi(command);
  28.  
  29.         if (counter % 3 != 0) {
  30.             dishes += detergentRefillInMililiters;
  31.             detergentQuantity -= detergentRefillInMililiters * 5;
  32.         }
  33.         else {
  34.             pots += detergentRefillInMililiters;
  35.             detergentQuantity -= detergentRefillInMililiters * 15;
  36.         }
  37.        
  38.         if (detergentQuantity < 0) {
  39.             cout << "Not enough detergent, " << abs(detergentQuantity) << " ml. more necessary!\n";
  40.             break;
  41.         }
  42.        
  43.         counter++;
  44.     }
  45.  
  46.     return 0;
  47. }
  48.  
  49. РЕШЕНИЕ С FOR:
  50.  
  51. #include <iostream>
  52. #include <string>
  53.  
  54. using namespace std;
  55.  
  56. int main() {
  57.     const int BOTTLED_DETERGENT_IN_MILILITERS = 750;
  58.  
  59.     int detergentCount, dishes = 0, pots = 0;
  60.     cin >> detergentCount;
  61.  
  62.     int detergentQuantity = detergentCount * BOTTLED_DETERGENT_IN_MILILITERS;
  63.  
  64.     string command;
  65.  
  66.     for (int counter = 1; counter < 2147483647; counter++) {  // INT_MAX = 2147483647
  67.  
  68.         cin >> command;
  69.  
  70.         if (command == "End") {
  71.             cout << "Detergent was enough!\n"
  72.                 << dishes << " dishes and " << pots << " pots were washed.\n"
  73.                 << "Leftover detergent " << detergentQuantity << " ml.\n";
  74.             break;
  75.         }
  76.  
  77.         int detergentRefillInMililiters = stoi(command);
  78.  
  79.         if (counter % 3 != 0) {
  80.             dishes += detergentRefillInMililiters;
  81.             detergentQuantity -= detergentRefillInMililiters * 5;
  82.         }
  83.         else {
  84.             pots += detergentRefillInMililiters;
  85.             detergentQuantity -= detergentRefillInMililiters * 15;
  86.         }
  87.        
  88.         if (detergentQuantity < 0) {
  89.             cout << "Not enough detergent, " << abs(detergentQuantity) << " ml. more necessary!\n";
  90.             break;
  91.         }
  92.     }
  93.  
  94.     return 0;
  95. }
  96.  
  97. РЕШЕНИЕ С FOR И ТЕРНАРЕН ОПЕРАТОР:
  98.  
  99. #include <iostream>
  100. #include <string>
  101.  
  102. using namespace std;
  103.  
  104. int main() {
  105.     const int BOTTLED_DETERGENT_IN_MILILITERS = 750;
  106.  
  107.     int detergentCount, dishes = 0, pots = 0;
  108.     cin >> detergentCount;
  109.  
  110.     int detergentQuantity = detergentCount * BOTTLED_DETERGENT_IN_MILILITERS;
  111.  
  112.     string command;
  113.     cin >> command;
  114.  
  115.     for (int counter = 1; counter < 2147483647 && command != "End" && detergentQuantity >= 0; counter++) {
  116.  
  117.         counter % 3 == 1 ? dishes += stoi(command) : pots += stoi(command);
  118.         detergentQuantity -= (counter % 3 == 1 ? 5 : 15) * stoi(command);
  119.         cin >> command;
  120.     }
  121.  
  122.     command == "End" ?
  123.         cout << "Detergent was enough!\n"
  124.         << dishes << " dishes and " << pots << " pots were washed.\n"
  125.         << "Leftover detergent " << detergentQuantity << " ml.\n" :
  126.         cout << "Not enough detergent, " << abs(detergentQuantity) << " ml. more necessary!\n";
  127.  
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement