Problems in Nankai district (5)

Source: Internet
Author: User

/* Subject requirements: There are n people sitting around in a circle and numbered from 1 to n clockwise. The number of messages from 1 to m starts from s to m, this person goes out of the circle, and then starts the 1-m Report from the next person. This continues until all the people go out of the circle. The order of the n persons is given in the out-of-the-circle order. Ask the examinee to compile the function Joseph GH () to implement this function and call the function WriteDat () to output the numbers to the OUT. DAT file in the OUT-of-band order. Set n = 100, s = 1, m = 10 for programming. Analysis: Joseph's rings have many solutions, but they can be divided into two categories: 1. mark the person who meets the outbound rules, but do not enter the circle, but the next time it is his or her turn, he will skip it directly and will not participate in the Report 2. the person that meets the outbound requirements will be directed out (removed from the ring), and the remaining person will continue to report the number. The second method is listed here. The deletion method is to place the element to the last position in the array. The elements behind the element in the outer ring move one forward as follows: */[cpp] void Joseph GH (void) {int I; int count; // count is used as the number variable int people = n; // defines the number of students not circled int tem; // enter the number of the person in the bucket, int j. // enter the number of the person in the bucket for (I = 0; I <n; I ++) {p [I] = I + 1;} I = 0; count = 1; // The subscript of the number starts from scratch, and count starts from 1. That is to say, I is the number of the count, and count and I are added at the same time. // Before no one goes out of the circle, people = 100. Every person goes out of the circle, people-1. When only one person is left, the loop stops. While (people> 1) {I = I % people; // when the subscript of the person already exists is not calculated, that is, when the number of the last person in the array has no outbound number, I point to 0 count = count % m again; // count to 9 and then from scratch when count to 10, the count value is 0, execute the following if statement if (count = 0) {// The following loop moves the number following the person in the circle forward by one digit, the number of the person in the outer ring is placed in the last digit of the p array, which is regarded as the outer ring. At the same time, the number of people is reduced by one www.2cto.com tem = p [I]; for (j = I; j <people-1; j ++) {p [j] = p [j + 1];} p [j] = tem; people --; count ++; // Based on the question, count: returns the current position.} I ++; count ++ ;}}

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.