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.