Is it Joseph's problem?
Joseph question is an interesting math game, the rules of the game are as follows:
1, n individuals in a circle, numbering starting from 1, and then to N.
2, the game participants numbered m began to count off, count off from 1 onwards, behind the people to count Solitaire, until K, counted as K will be out of the people.
3. The next player of the exit is then counted off from 1, so loop until the end of the game when the player is left, this player is the game winner.
So the question is, which number is the game winner?
Here are some simple lines of Python code to solve this problem:
#!/usr/bin/env Python # joseph Problem def Joseph (Total, begins, count): queue = range (1, total + 1) death = (begi NS + count-2)% len (queue) for times in Range (TOTAL-1): print ' out: ', Queue[death] del Queue[death] D Eath = (death + count-1)% len (queue)
In the Joseph () function, the parameter total is the above mentioned N,begins namely M,count and K, each cycle count out a number, the last remaining survivor is the game winner.
The usual implementation of C + + is as follows:
#include
using namespace std;void main () {int n=0,c=0; cout<< "Please enter the number of people:n="; CI n>>n; cout<< "Please enter:c="; cin>>c; int i=0,j=0,n=n,s=0; int *a=new int [N]; For (i=0;i
<><"->
"; } else { cout<
<>
< pre="">
This is common in the C + + language test, the following program to enter the number of people from the console n,c and the elimination of the queue's personnel number sequentially output to the console.