[Daily Blue Bridge] 21. Four-year provincial Java group real question "Guess the letter"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Guess the letter

The sequence consisting of 19 letters of abcd...s is repeatedly spliced ​​106 times to obtain a string with a length of 2014.

Next, delete the first letter (that is, the initial letter a), as well as the 3rd, 5th and all odd-numbered letters.

After the new string is obtained, perform the action of deleting the odd-numbered letters, and so on, there is only one letter left. Please write the letter.

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

Problem-solving ideas:

According to the meaning of the question, we know that the requirement in the question is to remove the odd-numbered digits in the string of length 2014, and finally there is only one letter left and the process of finding the letter. Many people are the first when they see such a question. One thought is to use the form of a list to store this string with a length of 2014, because in this way, the remove method can be used to delete the characters on the odd digits, and the loop will continue until only one character is left. This method seems feasible. But in the actual operation, you will find that this is not the case. The reason is that each time you use the remove method to delete a character, the length of the list will be reduced by one, so that when you delete the next one, the deletion is not the previous odd number. This cannot guarantee that the length of the string and the odd-numbered characters are fixed each time through the loop.

So thinking of this, we can use an array instead. Although the array cannot directly delete the characters on the odd digits, it can move the characters on the even digits forward, and finally move all the even numbers to the front, assuming that every time you move k Even-numbered characters, then in the next move, you can use the first k bits of the array as a new array, and then repeat the operation of the first step. Until the last k=1, that is, when only one even-numbered digit is moved in the array, the character on the even-numbered digit is the final answer is the letter q.

Answer source code:

public class Year2014_Bt3 {
	public static void main(String[] args) {
		char[] c = new char[2014];	//存放所有的字母
		int index = 0;	//标记数组中元素下标
		//循环106次,将前19个字母存放到数组中
		for (int i = 1; i <= 106; i++) {
			for (int j = 0; j < 19; j++) {
				c[index++] = (char) ('a' + j);
			}
		}
		
		int len = 2014;
		//当有效数组长度不是1时,让偶数位前移
		while (len!=1) {
			int k=0;
			//将偶数位从0开始向后赋给数组
			for (int i = 1; i < len; i+=2) {
				c[k++] = c[i];
			}
			len = k;	//记录每次剩余的偶数位,也是下一次有效数组的长度
		}
		System.out.println(c[0]);
	}

}

 

 

Sample output:

There are deficiencies or improvements, and I hope that my friends will leave a message and learn together!

Interested friends can follow the column!

Little Gray Ape will accompany you to make progress together!

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/113527154