Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- int count(const std::string& str) {
- int result = 0;
- for (const char& c : str) {
- if (c == ' ') {
- result++;
- }
- }
- return result + 1;
- }
- std::string getPerson(const std::string& str, int index) {
- int start = 0;
- int spaceCount = 0;
- for (size_t i = 0; i < str.size(); i++) {
- if (spaceCount == index) {
- break;
- }
- if (str[i] == ' ') {
- spaceCount++;
- start = i + 1;
- }
- }
- int end = start + 1;
- while (end < str.size() && str[end] != ' ') {
- end++;
- }
- return str.substr(start, end - start);
- }
- int main() {
- std::string str;
- std::getline(std::cin, str);
- int words = count(str);
- str.clear();
- std::getline(std::cin, str);
- int kidsCount = count(str);
- int index;
- if (!(words % kidsCount)) {
- index = kidsCount - 1;
- } else {
- index = words % kidsCount - 1;
- }
- std::cout << getPerson(str, index);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement