Question: There are n people in a circle with sequential troubleshooting. When the first person reports the number (from 1 to 3), the person who reports the number 3 leaves the circle and asks the last person who left the first number?
1 package com. weidu. algorithms;
2
3 import java. util. Arrays;
4 import java. util. collections;
5
6 /**
7 * @ Title:
8 * @ Description:
9 * @ author Afei
10 * @ date: 05:41:04 pm
12 * @ version: 1.0
13 */
14 public class Qu3 {
15
16 /**
17 * Function Description: 1. There are n people in a circle and the order is numbered. If the number of reports starts from the first person (from 1 to 3), those who report to 3 will leave the circle. <br>
18 * the last question is the number of the original employee.
19 * www.2cto.com
20 * @ Afei 2012-6-10
21 * @ param args
22 */
23
24 public static void main (String [] args ){
25 // TODO Auto-generated method stub
26 bytes input = new partition (System. in );
27 System. out. print ("Enter the total number of people :");
28 int p = input. nextInt ();
29/***** initialization personnel ***/
30 boolean [] per = new boolean [p]; // The boolean array indicates the person standing in a circle, and false indicates that the user exits.
31 for (int I = 0; I <per. length; I ++ ){
32 per [I] = true;
33}
34
35/***** Report Number ***/
36 int t = 0, len = per. length;
37 while (len> 1 ){
38 for (int I = 0; I <per. length; I ++ ){
39
40 if (per [I]) {
41 t ++;
42 if (t = 3 ){
43 t = 0;
44 per [I] = false;
45 len --;
46}
47}
48}
49}
50/***** result *****/
51 System. out. println ("last case:" + Arrays. toString (per ));
52 for (int I = 0; I <per. length; I ++ ){
53 if (per [I]) {
54 System. out. println ("Number of original calls:" + (I + 1) % 3 );
55}
56}
57}
58}
59
Author: feezh