This uses an array to simulate the loop list
#include <stdio.h>
int main ()
{
int leftcount = 15; Number of remaining monkeys
int countnum = 0; Now count to the first few
int index = 0; Defines the index of the current array starting with 0.
int number = 7; Count to the seventh, and the monkey quits.
int a[15]; Altogether 15 monkeys
int k, I;
Initialize first so that everything is 1.
for (i = 0; i < i++)
{
A[i] = 1;
}
And then every kick out one is minus one in the rest of the total,
So when the rest of the total is one,
Then it means we've found the king.
while (leftcount! = 1)
{
When the element is 1, it is counted,
When this number reaches 7, it's time to kick the seventh one out.
if (a[index] = = 1)
{
If the number of currently remaining monkeys is greater than 1,
And then the tag is 1, then add 1 to the counter.
countnum++;
The number of counters and the number of monkeys kicked out of the same time,
Kick out the monkey and change the mark to 0.
if (Countnum = = 7) {
Countnum = 0; Refreshes the counter, initialized to 0
A[index] = 0; Change the current marker to 0
leftcount--; Minus one in the remaining monkeys.
}
}
Each time you finish judging an element, the array's subscript adds a
Index + +;
When the end of the array is judged, the loop list is simulated here,
The tail of the list, then the head, then the tail of the array, then the head
It changes the subscript of the last element of the array to 0,
That is, back to the first element.
if (index = = 15) {
index = 0;
}
}
So far the king has been found,
Then only the monkey with the element 1 is the king.
So, the loop looks for the 1?
for (k = 0; k <; k++)
{
if (a[k] = = 1) {
printf ("%d", K + 1);
}
}
return 0;
}
C language implements the monkey to choose the King (Joseph Question)