Advertisement
Spocoman

Tourist Shop

Sep 22nd, 2023 (edited)
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     double budget, price, totalSum = 0;
  8.     cin >> budget;
  9.     cin.ignore();
  10.  
  11.     string product;
  12.     getline(cin, product);
  13.  
  14.     int counter = 0;
  15.  
  16.     while (product != "Stop") {
  17.         cin >> price;
  18.         cin.ignore();
  19.         counter++;
  20.  
  21.         if (counter % 3 == 0) {
  22.             price /= 2;
  23.         }
  24.         totalSum += price;
  25.  
  26.         if (budget < totalSum) {
  27.             break;
  28.         }      
  29.         getline(cin, product);
  30.     }
  31.  
  32.     cout.setf(ios::fixed);
  33.     cout.precision(2);
  34.  
  35.     if (product == "Stop") {
  36.         cout << "You bought " << counter << " products for " << totalSum << " leva.\n";
  37.     }
  38.     else {
  39.         cout << "You don't have enough money!\n"
  40.             << "You need " << totalSum - budget << " leva!\n";
  41.     }
  42.  
  43.     return 0;
  44. }
  45.  
  46. Решение с for и printf():
  47.  
  48. #include <iostream>
  49. #include <string>
  50.  
  51. using namespace std;
  52.  
  53. int main() {
  54.     double budget, price, totalSum = 0;
  55.     cin >> budget;
  56.     cin.ignore();
  57.  
  58.     string product;
  59.     getline(cin, product);
  60.  
  61.     for (int i = 1; i < 2147483647; i++) {
  62.         if (product == "Stop") {
  63.             printf("You bought %i products for %.2f leva.\n", --i, totalSum);
  64.             return 0;
  65.         }
  66.         cin >> price;
  67.         cin.ignore();
  68.  
  69.         if (i % 3 == 0) {
  70.             price /= 2;
  71.         }
  72.         totalSum += price;
  73.  
  74.         if (budget < totalSum) {
  75.             printf("You don't have enough money!\nYou need %.2f leva!\n", totalSum - budget);
  76.             return 0;
  77.         }  
  78.         getline(cin, product);
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement