Problem description
There are n individuals in a circle, sequential automatic arranging (numbered 1 to N). Starting from the 1th person (from 1 to 3 off), where 3 of the people quit the circle. From the next person began to count off, until the last person left, the game is over.
The last one left was the number of the first.
For example, 8 people in a circle:
1 2 3 4 5 6 7 8
After the 1th time off, 3 exits, leaving:
1 2 4 5 6 7 8 (now starting from 4)
After the 2nd time off, 6 exits, leaving:
1 2 4 5 7 8 (now starting from 7)
After the 3rd time off, 1 exits, leaving:
2 4 5 7 8 (now starting from 2)
After the 4th time off, 5 exits, leaving:
2 4 7 8 (now starting from 7)
After the 5th time off, 2 exits, leaving:
4 7 8 (now starting from 4)
After the 6th time off, 8 exits, leaving:
4 7 (now starting from 4)
After the last count, 4 exits, leaving:
7.
So, the last person to stay is the number 7.
Input format
A positive integer n, (1<n<10000)
Output format
A positive integer, the number of the last person left.
Sample input
8
Sample output
7
Data size and conventions
For 100% of data, 1<n<10000.
Topic Analysis: For this regular topic to see after you do not rush to write code to carry out the analysis of the topic, write a simple solution steps:
while (ture)
{
for (i=1;i<=3;i++)
{
The subscript value of the array is added;
Judge whether the above is beyond the beginning of the first;
}
The corresponding array of i=3 in the logarithm group is labeled as J in the book deletion;
The length of the array is reduced by one;
if (array length ==1)
{
Jump out of the loop;
}
}
After that, the first element in the array is the last element left.
1#include <stdio.h>2 intMain () {3 voidFun (inta[10000],intNintlen);4 intNum5 inta[10000];6 inti,j;7 8scanf"%d",&num);9 intlen=num;Ten for(i=1, j=0; i<=num; i++) { Onea[j++]=i; A } -j=-1; - while(1) { the for(i=1; i<=3; i++) - { -j=j+1; - if(j>len-1) + { -j=0; + } A } at Fun (A,j,len); -j--; -len--; - - if(len==1) - { in Break; - } to } +printf"%d", a[0]); - return 0; the } * voidFun (inta[10000],intNintLen) { $ inti;Panax Notoginseng for(i=n;i<len-1; i++) - { thea[i]=a[i+1]; + } Aa[i]=' /'; the}
Blue Bridge Cup--digital screening