In: Computer Science
C++ PLEASE
In the children’s game, Duck, Duck, Goose, a group of children sit in a circle. One of them is elected “it” and that person walks around the outside of the circle. The person who is “it” pats each child on the head, saying “Duck” each time, until randomly reaching a child that the “it” person identifies as “Goose.” At this point there is a mad scramble, as the “Goose” and the “it” person race around the circle. Whoever returns to the Goose’s former place first gets to remain in the circle. The loser of this race is the “it” person for the next round of play. The game continues like this until the children get bored or an adult tells them it’s snack time. Write software that simulates a game of Duck, Duck, Goose.
=======================================================================================
Save below code as .cpp extension format :
=======================================================================================
#include<iostream>
#include<vector>
#include<string>
#include<numeric>
using namespace std;
int main() {
int testCaseCount {};
cin >> testCaseCount;
for (int i = 0;i < testCaseCount;i++) {
//Get input
int playerCount {};
cin >> playerCount;
int duckCount[playerCount - 1];
for (int j = 0;j < playerCount - 1;j++)
cin >> duckCount[j];
//Declare initial variables
vector<int> players(playerCount);
iota(players.begin(),players.end(),1); //Range of position
values
int currentPos = 0;
//Run simulation
for (int j = 0;j < playerCount - 1;j++) {
currentPos = (currentPos + duckCount[j]) % players.size();
players.erase(players.begin() + currentPos);
}
cout << "The winner student will be: " << players[0]
<< '\n';
}
return 0;
}
=======================================================================================
Below is the screen shot of above code with output :