Advertisement
OIQ

Untitled

OIQ
Oct 3rd, 2021
887
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. int count(const std::string& str) {
  5.     int result = 0;
  6.     for (const char& c : str) {
  7.         if (c == ' ') {
  8.             result++;
  9.         }
  10.     }
  11.  
  12.     return result + 1;
  13. }
  14.  
  15. std::string getPerson(const std::string& str, int index) {
  16.     int start = 0;
  17.     int spaceCount = 0;
  18.     for (size_t i = 0; i < str.size(); i++) {
  19.         if (spaceCount == index) {
  20.             break;
  21.         }
  22.         if (str[i] == ' ') {
  23.             spaceCount++;
  24.             start = i + 1;
  25.         }
  26.     }
  27.     int end = start + 1;
  28.     while (end < str.size() && str[end] != ' ') {
  29.         end++;
  30.     }
  31.  
  32.     return str.substr(start, end - start);
  33. }
  34.  
  35. int main() {
  36.     std::string str;
  37.     std::getline(std::cin, str);
  38.  
  39.     int words = count(str);
  40.  
  41.     str.clear();
  42.     std::getline(std::cin, str);
  43.  
  44.     int kidsCount = count(str);
  45.     int index;
  46.  
  47.     if (!(words % kidsCount)) {
  48.         index = kidsCount - 1;
  49.     } else {
  50.         index = words % kidsCount - 1;
  51.     }
  52.  
  53.     std::cout << getPerson(str, index);
  54.  
  55.     return 0;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement