Advertisement
Spocoman

Easter Eggs Battle

Sep 17th, 2023 (edited)
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int firstPlayer, secondPlayer;
  7.     cin >> firstPlayer >> secondPlayer;
  8.  
  9.     string command;
  10.     cin >> command;
  11.  
  12.     while (command != "End") {
  13.         if (command == "one") {
  14.             secondPlayer--;
  15.             if (secondPlayer == 0) {
  16.                 cout << "Player two is out of eggs. Player one has " << firstPlayer << " eggs left.\n";
  17.                 break;
  18.             }
  19.         }
  20.         else if (command == "two")
  21.         {
  22.             firstPlayer--;
  23.             if (firstPlayer == 0)
  24.             {
  25.                 cout << "Player one is out of eggs. Player two has " << secondPlayer << " eggs left.\n";
  26.                 break;
  27.             }
  28.         }
  29.         cin >> command;
  30.     }
  31.  
  32.     if (command == "End") {
  33.         cout << "Player one has " << firstPlayer << " eggs left.\n"
  34.             << "Player two has " << secondPlayer << " eggs left.\n";
  35.     }
  36.     return 0;
  37. }
  38.  
  39. Решение с тернарен оператор:
  40.  
  41. #include <iostream>
  42.  
  43. using namespace std;
  44.  
  45. int main() {
  46.     int firstPlayer, secondPlayer;
  47.     cin >> firstPlayer >> secondPlayer;
  48.  
  49.     string command;
  50.     cin >> command;
  51.  
  52.     while (command != "End") {
  53.         command == "one" ? secondPlayer-- : firstPlayer--;
  54.  
  55.         if (firstPlayer == 0 || secondPlayer == 0) {
  56.  
  57.             cout << "Player " << (firstPlayer == 0 ? "one" : "two")
  58.                 << " is out of eggs. Player " << (firstPlayer > 0 ? "one" : "two")
  59.                 << " has " << (firstPlayer > 0 ? firstPlayer : secondPlayer) << " eggs left.\n";
  60.             break;
  61.         }
  62.         cin >> command;
  63.     }
  64.  
  65.     if (command == "End") {
  66.         cout << "Player one has " << firstPlayer << " eggs left.\n"
  67.             << "Player two has " << secondPlayer << " eggs left.\n";
  68.     }
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement