Data structure Practice--Monkey Selection King (array version)

Source: Internet
Author: User

This article is aimed at the data structure Basic Series Network course (5): Array and generalized table practice project.

"Project-Monkey King (array version)"

A group of monkeys, numbering is three-way ... m, the monkeys (m) sit around in the order of 1-m. Starting from the 1th number, every number to Nth, the monkey will leave this circle, so in turn, the last one out of the circle of the monkey as king. Input m and n, output the order of monkeys out of the circle, it can also be seen from the last king is the number of monkeys.
Requires an array to be used as the storage structure.

[Refer to answer 1]

In an array, the array with 1 means the monkey in the circle, with 0 to indicate that the monkey has been out of the circle, the array subscript corresponding to the monkey number corresponding to (for example, the array element p[0] value is 1, indicating that the 1th monkey is still in the circle, that is, p[i] for the number of i+1 monkeys in the circle).
A monkey out of the loop, the corresponding array value is set to 0, in the process, to cross the value of 0 monkeys.
If m=8, n=4, the initial array is as follows:

With 3 monkeys out of the loop, the values in the array are as follows:

Count to the last monkey to go back to the position of the subscript 0, after the monkey out of the circle, but also the corresponding element value is set to 0. See code comments.

#include <stdio.h>#define MAXSIZE 8voidKingintMintN) {intP[maxsize];intI,j,t; for(i=0; i<m; i++)//Build initial sequence, record M monkey in Circlep[i]=1; t=-1;//The first count will start at 0, that is, the 1th monkey, since T is 1 before using p[t]    printf("dequeue Order:"); for(i=1; i<=m; i++)//Cycle to execute m times, there are M monkeys to be out of the circle{j=1;//J for count off         while(j<=n)//{t= (t+1)%m;//Look at the next monkey, to arrive at the end of the fold back, so with%m            if(p[t]==1) j + +;//equal to if (p[t]==1) j + +; only if the Q monkey is in the circle, this position is counted.} p[t]=0;//Monkey out Circle        printf("%d", t+1);//Output The number of the ring monkey}printf("\ n");}intMain () {intM,n;scanf("%d%d", &m, &n); King (M,n);return 0;}
[Refer to answer 2]

Array with reference answer 1. In the process, no longer judge 0 is 1, but set up a variable for the accumulation, the monkey in the lap and 1 is equivalent to a count, after the ring is added 0 equivalent to no count.

#include <stdio.h>#define MAXSIZE 8voidKingintMintN) {intP[maxsize];inti,s=0, t; for(i=0; i<m; i++)//Build initial sequence, record M monkey in Circlep[i]=1; t=0;//The first count starts at 0 and is the 1th monkey    printf("dequeue Order:"); for(i=1; i<=m; )//Cycle to execute m times, there are M monkeys to be out of the circle{s=s+p[t];//s accumulation, the monkey in the lap plus 1 equivalent to count off, after the ring is added 0 equivalent to no count.         if(s==n) {p[t]=0;//Monkey out Circle            printf("%d", t+1);//Output The number of the ring monkeys=0;//Start accumulating againi++;//Count the Monkey plus 1} t= (t+1)%m;//Again, start with the next monkey}printf("\ n");}intMain () {intM,n;scanf("%d%d", &m, &n); King (M,n);return 0;}
[Refer to answer 3]

Save the monkey's number with an array element, a monkey out of the loop, perform an operation that removes the element from the array, repeating it.
If m=8, n=4, the initial array is as follows:

With 3 monkeys out of the loop, the values in the array are as follows:

Count to the last monkey to go back to the position of subscript 0, after the monkey out of the circle, but also to implement the deletion of elements in the array (the data will be moved forward) work. See code comments.

#include <stdio.h>#define MaxSizevoidKingintMintN) {intP[maxsize];intI,j,t; for(i=0; i<m; i++)//Build initial sequence, record M monkeys in p[0]~p[m-1]p[i]=i+1; t=0;the starting position for the first count is 0    printf("dequeue Order:"); for(I=m; i>=1; i--)//Cycle to execute M, there are M monkeys to be out of the circle; Total I descending from M to 1,i also indicates the number of monkeys in the circle{t= (t+n-1)%i;//starting from T number 1, then the first n-1 will count to N,t plus n-1 with the%i to take the remainder, the purpose is to reach the last monkey can fold back to continue to count        printf("%d", P[t]);///element with number P[t] Dequeue         for(j=t+1; j<=i-1; J + +)///The element that follows moves forward one position, deleting the monkey with the number p[t]p[j-1]=P[J]; }printf("\ n");}intMain () {intM,n;scanf("%d%d", &m, &n); King (M,n);return 0;}
Attached: With reference to answer 1 ideas, but the wrong program
//The following program has a bug, as a reverse case#include <stdio.h>#define MAXSIZE 8voidKingintMintN) {intP[maxsize];intI,j,t; for(i=0; i<m; i++)//Build initial sequence, record M monkey in Circlep[i]=1; t=0;//The first count starts at 0 and is the 1th monkey    printf("dequeue Order:"); for(i=1; i<=m; i++)//Cycle to execute m times, there are M monkeys to be out of the circle{j=1;//J for count off         while(j<n) {if(p[t]==1) j + +;//equal to if (p[t]==1) j + +; only if the Q monkey is in the circle, this position is counted.T= (t+1)%m;//Look at the next monkey, to arrive at the end of the fold back, so with%m}//Error Reason: The above only count to the first n-1, but there is no guarantee that the next must be in the circle. Here you can add a loop to find the next monkey in the circle, but obviously no reference answer 1 concisep[t]=0;//Monkey out Circle        printf("%d", t+1);//Output The number of the ring monkeyT= (t+1)%m;//Again, start with the next monkey}printf("\ n");}intMain () {intM,n;scanf("%d%d", &m, &n); King (M,n);return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure Practice--Monkey Selection King (array version)

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.