The queue algorithm of decrypting QQ number

Source: Internet
Author: User
Tags valid

The beginning of the new semester, Xiao Ha is a small hum of the new deskmate (small ha is a little beauty oh ~), small hum to ask the QQ number, small ha of course not directly tell Xiao hum, reason why you understand. So Xiao Ha gave the small hum a string of encrypted numbers, while Xiao Ha also told the small hum decryption rules. The rule is this: first delete the 1th number, then put the 2nd number to the end of the number, then delete the 3rd number and then put the 4th number to the end of the number, and then delete the number of 5th ... Until the last number is left, the last number is also deleted. According to the order just deleted, the number of these deletions linked together is the QQ of Xiao Ha. Now you help the little hum. Xiao Ha gave the little hum the number of encrypted strings is "6 3 1 75 8 9 2 4".

OK, now it's your turn to do it. Find out 9 notes or small pieces of paper, write "6 3 1 75 8 9 2 4" On 9 notes respectively, and simulate the decryption process. If you do not understand the wrong decryption rules, after the decryption of the QQ number should be "6 1 5 94 7 2 8 3".

In fact, the process of decryption is like to "queue" these numbers. Take two from the front, throw the 1th one away, and the 2nd one to the tail. The specific process is this: the first number is "6 3 1 75 8 9 2 4", first delete 6 and place 3 at the end of the string number, which is updated to "1 7 5 89 2 4". Next, delete 1 and put 7 to the end, which is updated to 5 8 9 24 3 7. Then delete 5 and put 8 at the end of "9 2 4 3 7 8", delete 9 and place 2 at the end of "4 3 7 8 2", delete 4 and put 3 to the end of "7 8 2 3", delete 7 and put 8 to the end of "2 3 8", delete 2 and put 3 to the end of "8 3", delete 8 and put 3 to the end of "3", the most Delete 3 after. So the order of deletion is "6 1 5 9 4 7 2 8 3", this is Xiao Ha's QQ number, you can add her to try ^_^.

Now that we have figured out the decryption law, I think you can try to program it, I believe you will be able to write it.

First you need an array to store this number of strings, i.e. intq[101]. and initialize this array that is intq[101]={0,6,3,1,7,5,8,9,2,4}; (here initialization is that I wrote more than 0, used to populate q[0], because I prefer to start with q[1], the array initialization is not very understanding students can go to see my last book, ah ha c! Think fast you step ") next is the process of simulating decryption.

The first step in decrypting is to delete the first number, and you can think about how to delete a number in the array? The easiest way to do this is to move all the back numbers forward one bit, overwriting the previous number. Just like we are in line to buy tickets, the front of the people to buy away, all the people in the back need to take a step forward, fill up before the vacancy, but this practice is time-consuming.

Here, I'll introduce two integer variable head and tail. The head (the first bit) used to record the queue, tail the next position of the queue's tail (the last one). You may ask why tail not record the tail of the team, but to record the next position at the end of the team? This is because when there is only one element left in the queue, the team head and tail overlap will cause some trouble. We're here to set the head and tail of the team to coincide, the queue is empty.

Now there are 9 numbers, 9 numbers are all placed in the queue after head=1;tail=10; the number between head and tail is the number of "valid" in the current queue. If you want to delete a number, the head++ is ok so that you can still keep the number between head and tail as "valid" in the current queue. While this is a waste of space, it saves a lot of time, which is very cost-effective. A new number is also very simple, the need to increase the number of the team at the end of the Q[tail] and then tail++ on the oak.

Let's make a summary of the operation of deleting a number at the head of the team is head++;

The operation of adding a number at the end of the team (assuming that the number is X) is q[tail]=x;tail++;

The entire decryption process, please see below this domineering external leakage of the figure.

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.