“There are people standing in a circle waiting to be executed. After the first man is executed, certain number of people are skipped and one man is executed. Then again, people are skipped and a man is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last man remains, who is given freedom.
The task is to choose the place in the initial circle so that you survive (are the last one remaining).”
— Wikipedia, http://en.wikipedia.org/wiki/Josephus_problem
Assume that the number of people, P, in the circle may be any number between zero and one hundred.
Assume that every Nth person around the circle is killed each turn, where N is an integer between one and twenty.
- Create an application in C++ that uses a linked list to represent the circle of people, numbered from 1 to P.
- Acquire the values P and N from the user at runtime via console input.
- Output the the individual that survives the mass execution.
There are several Java applet versions of this problem to check your work as you debug.
- This applet lets you choose how many people are in the circle and tells you who survives if every n die
There are two ways to interpret the “Skip Number” in the problem … if you for instance set 2 as the skip number that could mean “eliminate every other person” or “skip two then eliminate a person”
Interpretation 1 – 1 2 3 4 5 6 – Skip = 2 … Elimination order: 2, 4, 6, 3, 1 Survivor 5
Interpretation 2 – 1 2 3 4 5 6 – Skip = 2 … Elimination order: 3, 6, 4, 2, 5 Survivor 1
Please indicate in your User Interface how you interpret the “Skip Number”
The example here http://webspace.ship.edu/deensley/flash/JosephusProblem.html – uses interpretation 1
The starting person counts for your skip as shown in the above examples.