//User input int numberOfPeople = 1000; //Number of people in a group (starts counting at 0) int nthPersonKilled = 7; //How many people to skip before the killing (if counting from 1); if counting from 0, this is the person that will be killed. //end of user input int person[] = new int[numberOfPeople]; //create array of people int loopNumber = 0; //used to count how many alive people have been skipped int personCounter = 0; //used to determine position in the circle int sumOfLoop = 1; //If this is greater than 0, there is someone alive and the program will keep looping. If < 1, then it will conclude and print winner. int lastKilled; //Used for theatrics. It is the last person who was killed void keepInCheck() { //Makes the array a circle (loops at or out of bounds person to beginning) if (personCounter >= numberOfPeople) { personCounter = (personCounter - numberOfPeople); } } void setup() { //Set all people to 1 (alive). 0 is dead for (int i=0; i < numberOfPeople; i++) { person[i] = 1; } //Theatrics print(numberOfPeople); print(" people begin\n"); //Main loop while (sumOfLoop > 0) { //If there is still at least one person alive if (loopNumber == nthPersonKilled) { //If the appropriate number of living people have been skipped over if (person[personCounter] == 1) { //If the person is alive person[personCounter] = 0; //This kills the person //Theatrics print("Person "); print(personCounter); print(" has been killed\n"); print(sumOfLoop - 1); print(" remain alive\n"); //Cleanup loopNumber = 0; //reset person skipped counter lastKilled = personCounter; //Only used to print who has won. Can be taken out to improve performance. keepInCheck(); //If on last person, the circle will begin again } else { //If the person who should be killed is dead, move on personCounter++; //Advances keepInCheck(); //Wraps if applicable } } else if (person[personCounter] == 1) { //If the person is not the one to kill, check if they are alive personCounter++; //advance loop loopNumber++; //Count them as skipped over keepInCheck(); //Wrap if necessary } else { //If the person is not the one to kill and is also dead personCounter++; //keep counting keepInCheck(); //Wrap if necessary } //Count the number of people remaining sumOfLoop = 0; //Zero the sum for (int i=0; i < numberOfPeople; i++) { //add all values of the array sumOfLoop += person[i]; } } //Theatrics print("Person "); print(lastKilled); print(" was last alive\n"); print("Program Finished"); exit(); }