Carefree and desperate places in the circle

Source: Internet
Author: User

1. Question proposal

One department of the college needs to select half of the 154 students in the department, that is, 77 people, to participate in the summer camp. Due to the enthusiastic registration, the dispute persists, department of Student Union Chairman Wang Xiaoming proposed to implement screening based on student ID circle and report number Elimination: students whose student ID is 1, 2 ,.., 154, which is arranged in a circle in the clockwise direction of the student ID. James's student ID is No. 1, which starts from No. 1 in the counterclockwise direction of 1, 2,..., report number, where the report number M is out of the circle, so that the report number will continue to be eliminated. Until the last 77 Members are determined to join the summer camp.

To ensure fairness, the Count m is usually determined by the double dice, and the sum of the two odds points (obviously a positive integer in the range [2, 12] is determined as the Count M. For example, the sum of the two-color sub-points is 8, that is, to press 1, 2, 3... 8. Return the number of times in the clockwise direction. If the number of times is 8, the number of times out of the loop is eliminated.

The three experts in computer interest groups are excited after computing this selection method. Kobayashi thinks that he is lucky enough to have a worry-free position: No matter what number M is in 2, 3,..., 12, he can make sure that he will not be eliminated. No. Chen is bigger than Xiaolin, and he is also carefree. John said he was desperate: no matter what the number of M is in 2, 3,... 12, he will be eliminated. Excuse me:

① What are the student numbers of Xiao Lin, Xiao Chen, and Xiao Zhang?

② If the number of students is extended to any three even numbers, then half of the students will be screened according to this method. When the number of students is small, what is Xiao Ming's first question? When the number of students is small, James's 1 is desperate?

 

2. Design Ideas

Set array a [n]. The first array has an initial value of 1. At the same time, set the number of messages M (2 ~ 12) cyclically, the number of reports is implemented for each M. Each report contains one person, and the variable W is increased by 1. when the value of the variable w after a (I) is m, set a (I) = 0, and the flag number is I. Set y to count the number of people in the circle. At the same time, when W is 0 and the counter-clockwise direction is re-performed, the number of results is accumulated.

When I is increased to n + 1, I = 1.

When the number of people out of the circle reaches n/2, n/2 people are left before the number of people out of the circle ends.

Then, the array S (I) is used to calculate the number of times I is eliminated, and the value of S (I) is initialized to 11. During the reporting process, if I is) --. For m = 2, 3 ,..., 12. If S (I) = 11 (that is, the number of 11 reports is not eliminated each time), bitwise turns to Carefree. If S (I) = 0 (that is, 11 reports are reported, and each time they are eliminated), position I is the desperate position.

 

3. Code Implementation

 

// No worries and despair in the circle <br/> # include "stdafx. H "</P> <p> # define maxn 1000 </P> <p> int main (void) <br/> {<br/> // y indicates the number of people out of the circle, and N indicates the number of people, M indicates the maximum number of records. <br/> // W indicates that 1 is added for each report. <br/> int y, W, I, n, m; <br/> static int s [maxn], a [maxn]; </P> <p> printf ("Enter the number of students :"); <br/> scanf ("% d", & N); <br/> for (I = 1; I <= N; I ++) <br/> S [I] = 11; </P> <p> for (m = 2; m <= 12; m ++) <br/> {<br/> for (I = 1; I <= N; I ++) <br/> A [I] = 1; <br/> Y = 0; W = 0; I = 1; <B R/> while (Y <n/2) <br/>{< br/> if (a [I]) <br/> {<br/> W + = A [I]; // returns the number in a counter-clockwise order <br/> If (W = m) // 0 is assigned to the M reporter. <br/>{< br/> A [I] = 0; <br/> W = 0; <br/> Y ++; <br/> S [I] --; <br/>}< br/> I ++; <br/> if (I> N) <br/> I = 1; // returns again from scratch <br/>}</P> <p> for (I = 1; I <= N; I ++) <br/> {<br/> If (11 = s [I]) <br/> printf ("/n no worries: % d/N ", I); <br/> If (! S [I]) <br/> printf ("Despair bit: % d/N", I); <br/>}</P> <p> return 0; <br/>}

 

4. specify the number of people with even numbers in a specified range as the unlimited bit and the desperate bit for exploration

Based on the above carefree and desperate design, add a n-cycle. For the number X of n persons, if M is equal to 2, 3,..., 12, the number of 11 S is eliminated, that is, S (x) = 0, and the number of n persons is output in despair.

 

5. Code

 

// Specify the number in the even consortium in the respect to be a carefree and desperate exploration <br/> # include "stdafx. H "</P> <p> # define maxn 3000 </P> <p> int main (void) <br/>{< br/> int c, d, y, W, I, n, m, X; <br/> static int s [maxn], a [maxn]; </P> <p> printf ("Enter the number of students:"); <br/> scanf ("% d", & C, & D ); <br/> printf ("Enter the specified number:"); <br/> scanf ("% d", & X); <br/> If (C <X) // make sure that the starting point C is not less than the number x <br/> C = x; <br/> If (C % 2) // make sure that C is an even number <br/> C ++; <br/> for (n = C; n <= D; n + = 2) {<br/> for (I = 1; I <= N; I ++) <br/> S [I] = 11; <br/> for (m = 2; m <= 12; m ++) {<br/> for (I = 1; I <= N; I ++) <br/> A [I] = 1; <br/> Y = 0; W = 0; I = 1; <br/> while (Y <n/2) {// when the number of people out of the circle is less than n/2, continue to report <br/> if (a [I]) {<br/> W + = A [I]; // returns the number in a counter-clockwise order <br/> If (W = m) {<br/> A [I] = W = 0; <br/> Y ++; <br/> S [I] --; <br/>}< br/> I ++; <br/> if (I> N) <br/> I = 1; <br/>}< br/> If (11 = s [x]) <br/> printf ("/n when the number of students is % d, the" % d "parameter is a carefree position. ", N, x); <br/> If (! S [x]) <br/> printf ("/n when the number of students is % d, % d is the desperate bit. ", N, x); <br/>}</P> <p> return 0; <br/>}

Go to interesting C Program Design Series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.