Advertisement
Spocoman

06. Cinema Tickets

Sep 12th, 2023
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. РЕШЕНИЕ С WHILE:
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     int standardTicketCount = 0, studentTicketCount = 0, kidTicketCount = 0;
  10.  
  11.     string movie;
  12.     getline(cin, movie);
  13.  
  14.     while (true) {
  15.         if (movie == "Finish") {
  16.             break;
  17.         }
  18.  
  19.         int seats = 0, counter = 0;
  20.         cin >> seats;
  21.  
  22.         string category;
  23.  
  24.         while (counter != seats) {
  25.             cin >> category;
  26.             if (category == "End") {
  27.                 break;
  28.             }
  29.             else if (category == "standard") {
  30.                 standardTicketCount++;
  31.             }
  32.             else if (category == "student")
  33.             {
  34.                 studentTicketCount++;
  35.             }
  36.             else {
  37.                 kidTicketCount++;
  38.             }
  39.  
  40.             counter++;
  41.         }
  42.  
  43.         printf("%s - %.2f%% full.\n", movie.c_str(), 1.0 * counter / seats * 100);
  44.  
  45.         cin.ignore();
  46.         getline(cin, movie);
  47.     }
  48.  
  49.     int sumTickets = studentTicketCount + standardTicketCount + kidTicketCount;
  50.  
  51.     printf("Total tickets: %i\n", sumTickets);
  52.     printf("%.2f%% student tickets.\n", 1.0 * studentTicketCount / sumTickets * 100);
  53.     printf("%.2f%% standard tickets.\n", 1.0 * standardTicketCount / sumTickets * 100);
  54.     printf("%.2f%% kids tickets.\n", 1.0 * kidTicketCount / sumTickets * 100);
  55.  
  56.     return 0;
  57. }
  58.  
  59. РЕШЕНИЕ С WHILE, FOR И ТЕРНАРЕН ОПЕРАТОР:
  60.  
  61. #include <iostream>
  62. #include <string>
  63.  
  64. using namespace std;
  65.  
  66. int main() {
  67.     int standardTicketCount = 0, studentTicketCount = 0, kidTicketCount = 0;
  68.  
  69.     string movie;
  70.     getline(cin, movie);
  71.  
  72.     while (true) {
  73.         if (movie == "Finish") {
  74.             break;
  75.         }
  76.  
  77.         int seats = 0;
  78.         cin >> seats;
  79.  
  80.         string category;
  81.         int counter = seats;
  82.  
  83.         for (int i = 0; i < seats; i++) {
  84.             cin >> category;
  85.             if (category == "End") {
  86.                 counter = i;
  87.                 break;
  88.             }
  89.  
  90.             category == "standard" ? standardTicketCount++ :
  91.                 category == "student" ? studentTicketCount++ : kidTicketCount++;
  92.         }
  93.  
  94.         printf("%s - %.2f%% full.\n", movie.c_str(), 1.0 * counter / seats * 100);
  95.  
  96.         cin.ignore();
  97.         getline(cin, movie);
  98.     }
  99.  
  100.     int sumTickets = studentTicketCount + standardTicketCount + kidTicketCount;
  101.  
  102.     printf("Total tickets: %i\n", sumTickets);
  103.     printf("%.2f%% student tickets.\n", 1.0 * studentTicketCount / sumTickets * 100);
  104.     printf("%.2f%% standard tickets.\n", 1.0 * standardTicketCount / sumTickets * 100);
  105.     printf("%.2f%% kids tickets.\n", 1.0 * kidTicketCount / sumTickets * 100);
  106.  
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement