Advertisement
Spocoman

06. Cake

Sep 11th, 2023
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     int x, y;
  8.     cin >> x >> y;
  9.  
  10.     int cakePieces = x * y;
  11.    
  12.     string command;
  13.     cin >> command;
  14.  
  15.     while (cakePieces > 0 && command != "STOP") {
  16.         cakePieces -= stoi(command);
  17.         cin >> command;
  18.     }
  19.  
  20.  
  21.     if (cakePieces >= 0){
  22.         cout << cakePieces << " pieces are left.\n";
  23.     }
  24.     else {
  25.         cout << "No more cake left! You need " << abs(cakePieces) << " pieces more.\n";
  26.     }
  27.  
  28.     return 0;
  29. }
  30.  
  31. РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
  32.  
  33. #include <iostream>
  34. #include <string>
  35.  
  36. using namespace std;
  37.  
  38. int main() {
  39.     int x, y;
  40.     cin >> x >> y;
  41.  
  42.     int cakePieces = x * y;
  43.    
  44.     string command;
  45.     cin >> command;
  46.  
  47.     while (cakePieces > 0 && command != "STOP") {
  48.         cakePieces -= stoi(command);
  49.         cin >> command;
  50.     }
  51.  
  52.     cakePieces >= 0 ?
  53.         cout << cakePieces << " pieces are left.\n" :
  54.         cout << "No more cake left! You need " << abs(cakePieces) << " pieces more.\n";
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement