[C # algorithm and data structure] Joseph Ring

Source: Internet
Author: User

Problem description:
 With n personsSitting around in a circle, the number of people who count to m is displayed, and then the next person in the column starts to report again, and the number of people who count to m is displayed again, until all users are listed. output in the column-out order.

//******************************
// Description: Joseph Ring
// Class Name: Joseph
// Author: Hong Xiaojun
// Time: 2004-12-27
//******************************

1 Public   Class Joseph PHAs
2 {
3 // Starts counting from the start user, and records the columns cyclically in the units of alter. The total number is total.
4 Public   Int [] JOSE ( Int Total, Int Alter, Int Start)
5 {
6 Int I, J, K =   0 ;
7
8 // The Count array stores the data in the column-out order.
9 Int [] Count =   New   Int [Total + 1 ];
10
11 // S array to store initial data
12 Int [] S =   New   Int [Total + 1 ];
13
14 // Assign an initial value to array S. The first person serial number is 0, and the second person is 1.
15 For (I =   0 ; I < Total; I ++ )
16 {
17S [I]=I;
18}  
19
20 // Stores data in the array count in the order of Columns
21 For (I = Total; I > =   2 ; I -- )
22 {
23 Start = (Start + Alter -   1 ) % I;
24 If (Start =   0 )
25 Start = I;
26 Count [k] = S [start];
27 K ++ ;
28 For (J = Start +   1 ; J <= I; j ++ )
29 S [J -   1 ] = S [J];
30 }
31
32 Count [k] = S [ 1 ];
33
34 // Result returned
35 Return Count;
36 }  
37 }  


AboveProgramBoth Microsoft Visual Studio. NET 2003 and Visual C #2005 express edition beta1 have been debugged.

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.