Java Algorithm questions-guess letters/kill games

Source: Internet
Author: User

Java Algorithm questions-guess letters/kill games

The questions are as follows:

Put abcd... A string consisting of 19 letters in s is spliced for 106 times, and the length is 2014.

Next, delete the first 1st letters (a), and the first 3rd and 5th letters in all odd positions.

The new string is then deleted with an odd number of letters. In this case, there is only one letter left. Please write the letter.

The answer is a lowercase letter. Submit the answer in a browser. Do not enter any additional content.

Public class: {public static void main (String [] args) {String str = "abcdefghijklmnopqrs"; String str1 = ""; for (int I = 0; I <106; I ++) {str1 = str1 + str;} System. out. println (str1.length (); boolean [] arr = new boolean [str1.length ()]; for (int I = 0; I <arr. length; I ++) {arr [I] = true; // If the subscript is TRUE, the letter is still in the circle.} int leftCount = str1.length (); int countNum = 0; int index = 0; while (leftCount> 1) {if (arr [index] = true) {// if (countNum % 2 = 0) in the circle) {// when the subscript is an even number, arr [index] = false; // This letter exits from the circle leftCount --; // the remaining number of letters minus one} countNum ++;} index ++; // for each reported number, the subscript plus if (index = str1.length () {// is the number of loops. When the subscript is greater than n, it indicates that the number of loops has been counted, index = 0; // set the subscript to zero and start again. CountNum = 0 ;}/// print the last for (int I = 0; I <str1.length (); I ++) {if (arr [I] = true) {System. out. println (I); // The output result indicates a subscript of 1023 (1,024th) letters, that is, q }}}}

Solution 2:

Public class guess Letter 1 {public static void main (String [] args) {String str2 = ""; String str = "abcdefghijklmnopqrs"; for (int I = 0; I <105; I ++) {str = str + "abcdefghijklmnopqrs";} System. out. println (str. length (); while (str. length ()! = 1) {for (int I = 0; I <str. length (); I ++) {if (I % 2 = 1) {str2 + = str. charAt (I) ;}str = str2; str2 = ""; System. out. println (str );}}}

The second type is better understood and the answer is easier to find.

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.