Java algorithm Questions--guess letters/killing games

Source: Internet
Author: User

The topics are as follows:

The 19-letter sequence of ABCD...S is repeated 106 times to get a string of length 2014.

Next, delete the 1th letter (the first letter a), and the 3rd, 5th, and all odd-numbered positions.

Get the new string and then delete the odd position letter action. In this case, there is only one letter left, please write the letter.

The answer is a lowercase letter, please submit the answer via the browser. Do not fill in any extra content.

 Public  class to guess the letter {     Public Static voidMain (string[] args) {StringStr="ABCDEFGHIJKLMNOPQRS"; String str1 =""; for(inti =0; I <106; i++) {str1 = str1 +Str; } System.out.println (Str1.length ());Boolean[] arr =New Boolean[Str1.length ()]; for(inti =0; i < arr.length; i++) {Arr[i] =true;//Subscript True indicates that the letter is still in the circle}intLeftcount = Str1.length ();intCountnum =0;int Index=0; while(Leftcount >1) {if(arr[Index] ==true) {//When in a circle                if(Countnum%2==0) {//subscript is evenarr[Index] =false;//The letter exits the Circleleftcount--;///number of remaining letters minus one} countnum++; }Index++;//per newspaper, subscript plus one            if(Index= = Str1.length ()) {//is the number of loops, when the current index is greater than N, the description has been counted a circle,                Index=0;//Set the subscript to zero to start again. Countnum =0; }        }//Print out the last          for(inti =0; I < str1.length (); i++) {if(Arr[i] = =true) {System.out.println (i);//The output indicates a letter labeled 1023 (1024th), which is: Q}        }    }}

The second method of solution:

 Public  class to guess the letter 1 {     Public Static voidMain (string[] args) {String str2 =""; StringStr="ABCDEFGHIJKLMNOPQRS"; for(inti =0; I < the; i++) {Str=Str+"ABCDEFGHIJKLMNOPQRS"; } System.out.println (Str. Length ()); while(Str. Length ()! =1) { for(inti =0; I <Str. Length (); i++) {ifI2==1) {str2 + =Str. charAt (i); }            }Str= STR2; STR2 =""; System.out.println (Str); }    }}

The second better understanding, the answer is easier to find

Java algorithm Questions--guess letters/killing games

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.