[Typical java Algorithm questions] There are n people in a circle with sequential troubleshooting. Start from the first person (from 1 to 3), where the report is 3...

Source: Internet
Author: User

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

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.