17 Monkeys in a circle, from some beginning to count 1-2-3-1-2-3-...... Reported "3" of the monkeys were eliminated, the game has been carried out into the circle only a monkey It is the Monkey King.
method One:
Tip: Use an array to record whether the monkey is in the circle: "1" in the circle, not in the circle as "0". It also simulates the count-off process by accumulating the values of the array elements, thus reducing the operation of the monkey in the circle.
The C + + code is as follows:
#include <iostream>
#include <vector>
using namespace std;
const int N = +;
Print n the state of a monkey ("1" means in the circle, "0" means not in the circle)
void Printvec (const vector<int> &v)
{for
(vector<int> :: Const_iterator it = V.begin (); It! = V.end (); ++it)
{
cout<<*it<< ';
}
cout<<endl;
}
int monkeyking (vector<int> v)
{
//n record how many more monkeys in the circle
int n = n;
int k = 0;
int i;
while (n > 1)
{for
(i=0;i<n;++i)
{
k + = V[i];
if (k = = 3)
{
V[i] = 0;
Printvec (v);
k = 0;
--n
;
}}} The last monkey subscript 1 for
(int i=0;i<n;++i)
{
if (1 = = V[i])
return i+1;
} int main ()
{
vector<int> V (n,1);
cout<< "Monkey King is the first" <<monkeyking (v) << "only." <<endl;
return 0;
}
Operation Result:
Method Two:
The disadvantage of method one is to scan all the monkeys that have been out of the loop each time, the time efficiency is lower. If you use an array element to store the next marker in the inner circle, you can easily find the next monkey in the circle. When a monkey's next monkey is himself, there is only one monkey in the circle, it is waiting for the king.
C + + code:
#include <iostream>
#include <vector>
using namespace std;
const int N = +;
int main ()
{
vector<int> V (n,0);
for (int i=0;i<n-1;++i)
{
v[i] = i+1;
}
int test = 0;
int last;
while (test! = V[test])
{for
(int. i=0;i<3;++i)
{Last
= v[test];
Test = V[last];
cout<< "<<test+1<<" only monkey 3 "<<endl;
V[last] = v[test];
Test = V[test];
}
}
cout<< "Monkey King is the first" <<test+1<< "only." <<endl;
return 0;
}
Operation Result:
Updata 2016.07.16
Method 3:
In N (0, 1, 2, 3 、......、 n-1) only monkeys, assuming that the monkey is deleted, the first deleted monkey number is (M-1)%n, recorded as K, then delete k after the remaining n-1 monkeys for 0, 1 、......、 k-1, k+1 、......、 n-1, and the next is from the K + 1 start counting. Equivalent to the remaining sequence species, the k+1 is at the front, thus forming k+1 、......、 n-1, 0, 1 、......、 k-1. The remainder of this sequence of n-1 numbers is then k+1 、......、 n-1, 0, 1 、......、 k-1 map to form a sequence 0, 1, 2 、......、 n-2 from 0 to N-2. Define the map as P, then p (x) = (x-k-1)%n. Reverse over 0, 1, 2 、......、 n-2 map to k+1 、......、 n-1, 0, 1 、......、 k-1, when P ' (x) = (x+k+1)%n, and k= (m-1)%n, so P ' (x) = (x+m)%n. The last monkey's number is 0, so x=0,n=1. For example, when m=3, P ' (0) = (0+3)%1=3, indicates that the last Monkey King is numbered 3 in the penultimate round (numbering is also starting from 0).
The C + + code is given below:
#include <iostream>
using namespace std;
n Monkeys, counted m is deleted
int lastremaining (unsigned int n, unsigned int m)
{
if (n<1 | | m<1) return-1;
Last said that the final monkey (Monkey King) in each round (first countdown to the 1th round, and then the second round, ... ) in its own ordinal
//countdown to the first round when there is only one monkey (Monkey King), numbered 0
int last = 0;
I is the countdown I order for
(int. i=2;i<=n;++i)
{Last
= (last+m)%i;
}
Return last+1;//+1 to conform to the custom, indicating that the number will be changed from 1 to start
}
int main ()
{
cout<<lastremaining (3) << Endl;
return 0;
}