Advertisement
Spocoman

01. Old Books

Sep 10th, 2023
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     string book, input;
  8.     getline(cin, book);
  9.     getline(cin, input);
  10.  
  11.     int counter = 0;
  12.  
  13.     while (input != "No More Books" && input != book) {
  14.         counter++;
  15.         getline(cin, input);
  16.     }
  17.  
  18.     if (input == "No More Books") {
  19.         cout << "The book you search is not here!\nYou checked " << counter << " books.";
  20.     }
  21.     else {
  22.         cout << "You checked " << counter << " books and found it.";
  23.     }
  24.    
  25.     return 0;
  26. }
  27.  
  28. Решение с for тернарен оператор:
  29.  
  30. #include <iostream>
  31. #include <string>
  32.  
  33. using namespace std;
  34.  
  35. int main() {
  36.     string book, input;
  37.     getline(cin, book);
  38.  
  39.     for (int i = 0; i < 2147483647; i++) {   // INT_MAX = 2147483647
  40.         getline(cin, input);
  41.  
  42.         if (input == "No More Books" || input == book) {
  43.  
  44.             input == book ?
  45.                 cout << "You checked " << i << " books and found it." :
  46.                 cout << "The book you search is not here!\nYou checked " << i << " books.";
  47.             break;
  48.         }
  49.     }
  50.    
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement