Killing games implementation of Java code

Source: Internet
Author: User

Some time ago, the company colleagues asked a killing games feeling or very interesting, for our program ape, as long as there are rules, everything can be expressed in code (the idea of the Cock Silk program ape).

The title is this: there are 1000 criminals, who are numbered sequentially (starting from 1). First, the judge asked an even number of people to stand up and kill. They are then numbered sequentially without disrupting the original order. Then the judge Jenchie the number of people to stand up and kill. One analogy. Q: What was the last person in the 1000? (Don't ask me why the judge wanted to kill someone like that) the following is a personal code, please enlighten me.

int tag = 1;//number of killings int num = 1000;//criminals in the first few arraylist<string> list =  new ArrayList<String> ();//list collection of criminals arraylist<string> list0 = new  Arraylist<string> ();//To kill the Criminal Code, to remove List0.add ("0") from the list;for  (int i = 1; i  <= num; i++)  {//the offender     list.add (i +  "");} while  (True)  {//set the person to be killed as 0    if  (tag % 2 == 1)  {//         for  (int i = 1; i <=  List.size ();  i++)  {            if  ( i % 2 == 0)  {                 list.set (i - 1, 0 +  "");            &nBSP;}         }    } else {         for  (Int i = 1; i <= list.size ();  i++ )  {            if  (i % 2  ==&NBSP;1)  {                 list.set (i - 1, 0 +  "");             }        }    }     list.removeall (LIST0);//Kill the criminals, re-number     tag ++;//kill Count plus 1     if  (List.size ()  == 1)  {//when there's only one left, output this number     system.out.print ( List.get (0). toString ());     return;    }}


Killing games implementation of Java code

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.