This chapter requires the following prerequisites: Implementation of the sequence table class in the sequential storage structure of a linear table _ Java
Today, we will use the sequence table class to solve the Joseph (Joseph) ring problem.
First of all, I will give a brief description of Joseph's ring problem: a judge in ancient times decided to sentence n prisoners to death. He had a absurd law that made them stand in a circle, starting from the count of individuals in S, every time the number of prisoners in D is counted, the suspects are pulled out for execution, and then D are counted from the next one, and the count is executed again ,......, We know that the last prisoner is forgiven.
Use the sequence table seqlist to solve the Joseph Ring problem. The algorithm is described as follows:
Create an ordered table Object List with n elements.
Start from the second element and count in sequence. Each time the number is d, the corresponding element is deleted.
Count and delete the element until the next element is left.
If index is set to represent the sequence table element number, when the index changes according to the following rule during counting, the number order table can be viewed as a ring structure:
index = (index + d-1) % list.length();
Specify generic e as a class when declaring and creating seqlist objects. The procedure is as follows:
Import Datastructure. linearlist. lList; import Datastructure. linearlist. seqlist; public class Joseph PHUs // solve the Joseph Ring problem {private lList <string> list; // store multiple objects in the Joseph Ring public Joseph (INT number, int start, int distance) // create and solve the Joseph ring. The parameter specifies the ring length, start position, and count {This. list = new seqlist <string> (number); // the sequence table element type is a string, specifying the capacity for (INT I = 0; I <number; I ++) This. list. add (new string (char) ('A' + I) + ""); // Add the string object system. out. print ("Joseph ring (" + number + "," + start + "," + distance + "),"); system. out. println (this. list. tostring (); // display the string of all objects in the sequence table to indicate int Index = start-1; // count start position while (this. list. length ()> 1) // loop {Index = (index + distance-1) % This. list. length (); system. out. print ("delete" + this. list. remove (index ). tostring () + ","); // Delete the specified location object system. out. println (this. list. tostring ();} system. out. println ("the forgiven is" + list. get (0 ). tostring ();} public static void main (string [] ARGs) {New Joseph PHUs (10, 1, 3 );}}
The program running result is as follows:
Joseph ring (10, 1, 3), (A, B, C, D, E, F, G, H, I, J)
Delete C, (A, B, D, E, F, G, H, I, J)
Delete F, (A, B, D, E, G, H, I, J)
Delete I, (A, B, D, E, G, H, J)
Delete B, (A, D, E, G, H, J)
Delete g, (A, D, E, H, J)
Delete A, (D, E, H, J)
Delete h, (D, E, J)
Delete E, (D, J)
Delete J, (d)
D
In the next chapter, I will discuss the chain storage structure of a linear table-writing of the node class of a single-chain table.