Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <string>
- #include <sstream>
- std::list<std::string> readInput(std::string &turns) {
- std::list<std::string> names;
- std::string line;
- getline(std::cin, line);
- std::istringstream istr(line);
- std::string name;
- while (istr >> name) {
- names.push_back(name);
- }
- getline(std::cin, turns);
- return names;
- }
- std::string hotPotato(const int turns, std::list<std::string>& names) {
- int currTurns = turns;
- std::list<std::string>::iterator it = names.begin();
- currTurns--; //by pointing the first is one turn
- while (names.size() != 1) {
- if (currTurns == 0) {
- currTurns = turns;
- currTurns--;
- std::cout << "Removed " << *it << std::endl;
- it = names.erase(it);
- if (it == names.end()) {
- it = names.begin();
- }
- continue;
- }
- ++it;
- if (it == names.end()) {
- it = names.begin();
- }
- currTurns--;
- }
- return *it;
- }
- int main()
- {
- std::string turnsStr;
- std::list<std::string> names = readInput(turnsStr);
- int turns = stoi(turnsStr);
- std::cout << "Last is " << hotPotato(turns, names);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement