Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- int firstPlayer, secondPlayer;
- cin >> firstPlayer >> secondPlayer;
- string command;
- cin >> command;
- while (command != "End") {
- if (command == "one") {
- secondPlayer--;
- if (secondPlayer == 0) {
- cout << "Player two is out of eggs. Player one has " << firstPlayer << " eggs left.\n";
- break;
- }
- }
- else if (command == "two")
- {
- firstPlayer--;
- if (firstPlayer == 0)
- {
- cout << "Player one is out of eggs. Player two has " << secondPlayer << " eggs left.\n";
- break;
- }
- }
- cin >> command;
- }
- if (command == "End") {
- cout << "Player one has " << firstPlayer << " eggs left.\n"
- << "Player two has " << secondPlayer << " eggs left.\n";
- }
- return 0;
- }
- Решение с тернарен оператор:
- #include <iostream>
- using namespace std;
- int main() {
- int firstPlayer, secondPlayer;
- cin >> firstPlayer >> secondPlayer;
- string command;
- cin >> command;
- while (command != "End") {
- command == "one" ? secondPlayer-- : firstPlayer--;
- if (firstPlayer == 0 || secondPlayer == 0) {
- cout << "Player " << (firstPlayer == 0 ? "one" : "two")
- << " is out of eggs. Player " << (firstPlayer > 0 ? "one" : "two")
- << " has " << (firstPlayer > 0 ? firstPlayer : secondPlayer) << " eggs left.\n";
- break;
- }
- cin >> command;
- }
- if (command == "End") {
- cout << "Player one has " << firstPlayer << " eggs left.\n"
- << "Player two has " << secondPlayer << " eggs left.\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement