Array of Joseph rings, array of Joseph rings

Source: Internet
Author: User

Array of Joseph rings, array of Joseph rings

Recently, I was busy improving my interview skills, and said, "improving my business capabilities. I feel that the future of the company is dark, and even the dawn of the silk is missing. Fortunately, I have a strong one. Thanks to mother. Because one of the simulated objects has a medium level of learning, and the class speaks a word... a word is omitted here, so I thought there was one of the students I taught. Then I chatted with him about QQ. How can I be so dedicated? O__O ".... I didn't expect this student to be ready to learn IOS now, but I have always wanted to learn unlearned technologies. It's amazing. This morning, he sent me a question: Array Implementation of Joseph ring. In order to pass my depressed mood and time, I did it. Finally, I found that a demo written on the internet is better than mine, so I just rearranged it and added comments. The question and solution are as follows:

Array Implementation of Joseph Ring

Joseph's question was raised by the ancient Roman historian Joseph, who participated in and recorded the uprising of Jews against Rome between 66-70 a.m. As a general, Joseph managed to hold the city of Jude for 47 days. After the city fell, he and 40 soldiers took refuge in a nearby cave. Where are they? The soldiers are excited and said: Surrender is not a matter of death. Therefore, Joseph suggested that everyone take turns to kill the people next to him, and this order was determined by the lottery. Joseph had a premeditated grasp
The last sign survived as one of the two survivors in the cave.

The specific description of the Joseph Ring problem is: The number is set to 1, 2 ,......, n (n> 0) People in n are grouped into a circle, starting from the first person to report data. When reporting data to m, the report is stopped, then raise the new report from the next person,

When reporting to m, stop reporting, and report m's out of the circle. So on, we know there is only one person left. When n and m are given at any time, an algorithm is designed to calculate the order of n people's outbound circles.

Assume n = 5, 5 people: 1, 2, 3, 4, 5,

The number is reported from 1st people. If the number m is 2, the person is killed by his companions and goes out.
The order of the killed persons is: 2, 4, 3, 1, 5.

 

The Code is as follows:

1 /// <summary> 2 // implement the array of Joseph Ring 3 /// </summary> 4 /// <param name = "n"> total number </param> 5 // <param name = "m"> number starting from number </param> 6 // <param name = "k"> the number of people counted out </param> 7 public static void Joseph HP (int n, int m, int k) 8 {9 10 // create an array 11 int [] array = new int [n]; 12 for (int I = 0; I <n; I ++) 13 {14 array [I] = I + 1; // assign a value to each array element in a loop, numbered 1, 2, 3, 4 ,...., n 15} 16 17 18 int count = 0; // number of records 19 20 Int number = n; // The total number of remaining records 21 22 while (number> 1) 23 {24 for (int I = 0; I <n; I ++) // cyclically traverse every element in the Data 25 {26 if (m! = 1) // if not starting from 1 27 {28 I = m-1; // starting from m for Members, m for this person is 1st people 29 m = 1; 30} 31 if (array [I] = 0) // judge whether the person of array [I] is out. if this person is out, continue the loop and judge the next 32 continue; 33 count ++; 34 if (count = k) // if the number is equal to 35 {36 Console. write (array [I] + ""); // output number 37 array [I] = 0; // mark this position as 038 count = 0; // number starting from 0 39 number --; // total number-1 40} 41} 42} 43 44 45 // The element whose number is not 0 is determined cyclically, that is, 46 for (int I = 0; I <n; I ++) 47 {48 if (array [I]! = 0) 49 {50 Console. Write (array [I]); 51 break; 52} 53} 54 55 Console. WriteLine (); 56}The array of the Joseph Ring implements 1 static void Main (string [] args) 2 {3 Console. writeLine ("Enter the total number of people:"); 4 int m = int. parse (Console. readLine (); 5 6 Console. writeLine ("Enter the number of reports:"); 7 int n = int. parse (Console. readLine (); 8 9 Console. write ("the total number is {0}, starting from {1}. When the number is {2}, the number of outgoing users is:", m, 1, n ); 10 Joseph HP (m, 1, n); 11 12 Console. readLine (); 13}Test code

 


The problem of using arrays to implement Joseph's ring

In fact, it is difficult to use arrays.
We recommend that you use a circular linked list, but I use an array to write it for you. I don't know if you will understand it.
Void shuzu (int n, int m, int s)
{// N indicates the number of elements, m indicates the number of elements to be deleted, and s indicates the number of elements to be deleted.
// At the same time, s is the beginning of the report.
Int a [], j, I;
For (I = 1; I <n + 1; I ++)
A [I] = I; // arrange
Printf ("\ n ");
For (I = n; I> = 2; I --) // I indicates the length of the current table (not 0 RMB in the array ).
{S = (s + s-1) % I; // it is to be deleted, and the redirection is also controlled.
If (s = 0) s = I; // when s = I, it is not switched, so s is not 0, but I
Printf ("% d", a [s]);
For (j = s; j <= I-1; j ++)
A [j] = a [j + 1]; // you can specify one place from s to delete s.
}
Printf ("\ n ");
Printf ("% d", a [I]);
}

Use arrays to implement the C language Joseph Ring Problem

# Include <stdio. h>

# Deprecision max 100

Int main ()

{

Int Joseph Phu [max];

Int N, M, begin;/* N: total number M: skip begin: begin */

Int flag = 0;/* If flag is equal to M, print the current number .*/

Int I;/* Contrl tht loop */

Printf ("Input N, M and begin :");

Scanf ("% d", & N, & M, & begin );

Printf ("\ n ");

Begin = begin-1;

/* Assignment the array and assignment the element not used 0 and finally display it .*/

For (I = 0; I <N; I ++)

{

Joseph Phu [I] = I + 1;

Printf ("% d", Joseph Phu [I]);

}

For (I = N; I <max; I ++)

Joseph Phu [I] = 0;

Printf ("\ n ");

/* Joseph Phu [begin] = Joseph Phu [begin-1] */

For (I = 2; I ++)

{

If (Joseph Phu [begin]! = Joseph Phu [N-1])/* If the current element is not equal to the last elemnet of array Joseph Phu, then the next element .*/

Begin ++;

Else

{

Begin = 0;/* If the current element is equal to the last element, then the first element .*/

While (Joseph Phu [begin] <0)/* When there are negative elements, skip them .*/

Begin ++;

If (I % M = 0)/* If satisfied, print the element and assignment it as negative, and increase the value of flag .*/

{

Printf ("% d", Joseph Phu [begin]);

Joseph Phu [begin] =-Joseph Phu [begin];

Flag ++;

}

If (flag = N)/* If flag is equal to total number, the loop is broken .*/

Break;

... The remaining full text>

Related Article

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.