[Sit on the toilet and look at the algorithm] algorithm 4: queue-decrypt the QQ number

Source: Internet
Author: User

At the beginning of the new semester, xiaoha is a new table of Xiaohan (xiaoha is a pretty girl ~), Tom asked Tom about the QQ number. Of course he wouldn't tell Tom directly. You know the reason. So xiaoha gave xiaohum a string of encrypted numbers, and xiaoha also told him the decryption rules. The rule is as follows: first, delete the number 1st, then put the number 2nd at the end of the string, then delete the number 3rd, and put the number 4th at the end of the string, delete 5th more ...... Until the last number is left, the last number is also deleted. In the order of deletion, connecting these deleted numbers is xiaoha's QQ. Now you can help me. A string of numbers encrypted by Tom is "6 3 1 75 8 9 2 4 ". OK. Now it's your turn to start. Find 9 tabs or small pieces of paper, and write the 9 Numbers "6 3 1 75 8 9 2 4" On 9 tabs respectively to simulate the decryption process. If you do not understand the decryption rules correctly, the QQ number of xiaoha after decryption should be "6 1 5 94 7 2 8 3 ".
In fact, the decryption process is like queuing these numbers ". Take two at the beginning, 1st and 2nd at the end. The specific process is as follows: at the beginning, the number of the string is "6 3 1 75 8 9 2 4". First, delete 6 and put 3 at the end of the number, this string number is updated to "1 7 5 89 2 4 3 ". Next, delete 1 and put 7 to the end, which is updated to "5 8 9 24 3 7 ". Delete 5 and put 8 to the end, that is, "9 2 4 3 7 8". Delete 9 and put 2 to the end, that is, "4 3 7 8 2 ", delete 4 and put 3 to the end, that is, "7 8 2 3". Delete 7 and put 8 to the end, that is, "2 3 8 ", delete 2, put 3 to the end, that is, "8 3", delete 8, put 3 to the end, that is, "3", and delete 3. Therefore, the order of deletion is "6 1 5 9 4 7 2 8 3". This is the QQ number of xiaoha. You can add it to try ^_^.
Now that you have figured out the decryption rules, you can try programming on your own. I believe you can write it out.
First, an array is required to store this string, that is, intq [101]. And initialize this array, that is, intq [101] = {,}; because I prefer to use q [1], and I am not very familiar with array initialization. You can refer to my previous book "aha C! Think about it.) The next step is to simulate the decryption process.
The first step of decryption is to delete the first number. How can you delete a number from the array? The simplest way is to move all the numbers behind the scenes to the front and overwrite the previous ones. It is like we are waiting in the queue to buy tickets. When the first person buys a ticket and leaves, all the people in the backend need to take a step forward and fill in the previous blank space. However, this method takes a lot of time.

 

Here, I will introduce two integer variables head and tail. Head is used to record the first (first) of the queue, and tail is used to record the next position of the end (last) of the queue. You may ask why tail does not directly record the team's tail, but records the next position at the team's end? This is because when only one element is left in the queue, it may cause some trouble to overlap between the first and the end of the queue. We specify that the queue is empty when the first and the end of the team are duplicated.
Now there are 9 numbers, 9 are all put into the queue after head = 1; tail = 10; then the number between head and tail is the number of "valid" in the current queue. If you want to delete a number, the head ++ will be OK, so that the number between the head and tail can still be "valid" in the current queue. Although this process wastes a lot of space, it saves a lot of time, which is very cost-effective. Adding and adding a number is also very easy. Put the number to be added at the end of the team, that is, q [tail], and then tail ++ to ouke. Let's conclude that the first operation to delete a number in the team is head ++; the operation to add a number at the end of the Team (assuming this number is x) is q [tail] = x; tail ++; for the entire decryption process, see the domineering diagram below. The final output is 6 1 5 94 7 2 8 3. The code implementation is as follows.
# Include <stdio. h> int main () {int q [102] = {,}, head, tail; int I; // initialize the queue head = 1; tail = 10; // There are nine elements in the queue. The tail is directed to the last position of the team. while (head <tail) // when the queue is not empty, execute the loop {// print the first team and release the first team printf ("% d", q [head]); head ++; // first add the number of new team leaders to the end of the team q [tail] = q [head]; tail ++; // then the first team head ++ ;} getchar (); return 0 ;}

 

How did the above Code run successfully? Now let's summarize the concept of queue. A queue is a special linear structure. It can only be deleted in the header of the queue) the insert operation is called "joining ". When there are no elements in the queue (I .e. head = tail), it is called an empty queue. In our daily life, many situations are consistent with the queue features. For example, the ticket buying window we mentioned previously is a queue. In this queue, new people always stand at the end of the queue. The sooner the new person arrives, the sooner he gets the ticket, the sooner he gets the ticket. This is the first person first served, we call it the First InFirst Out (FIFO) principle.
The queue will be the core data structure of the Bellman-Ford short circuit algorithm that gives priority to search and queue optimization in the future. Therefore, the three basic elements (an array and two variables) of the queue are encapsulated into a struct type, as shown below.
Struct queue {int data [100]; // the subject of the queue, used to store the content int head; // The first int tail; // The End Of The team };

 

We have defined a struct type above. We usually place it outside the main function. Please note that there is a; sign at the end of the struct definition. Struct is the keyword of the struct, and queue is the name we name for this struct. The struct has three members: integer array data, integer head, and integer tail. In this way, we can put these three parts together as a whole. You can understand this: we define a new data type, which is very powerful. Each variable defined by this new type can store an integer array and two integers at the same time.
With a new struct type, how do I define struct variables? It is very simple. This is the same as the way we previously defined variables, as shown below.
struct queue q;

 

Please note that struct queue must be used as a whole. You cannot directly write queue q. In this way, we define a struct variable q. This struct variable q can satisfy all the operations in the queue. Then how can we access the internal members of struct variables? You can use the. Sign, which is called the member operator or the dot operator, as follows:
q.head=1;q.tail=1;scanf("%d",&q.data[q.tail]);

 

Well, the following code uses struct to perform queue operations.
# Include <stdio. h> struct queue {int data [100]; // queue body, used to store the content int head; // The first int tail; // team end}; int main () {struct queue q; int I; // initialize queue q. head = 1; q. tail = 1; for (I = 1; I <= 9; I ++) {// insert nine scanf ("% d", & q. data [q. tail]); q. tail ++;} while (q. head <q. tail) // when the queue is not empty, execute the loop {// print the first team and release the first team printf ("% d", q. data [q. head]); q. head ++; // Add the number of new team leaders to the end of the q. data [q. tail] = q. data [q. head]; q. tail ++; // start the q. head ++;} getchar (); return 0 ;}

 

Although this method seems redundant, it can enhance your understanding of the queue algorithm. The C ++ STL library already has queue implementation. If you are interested, please refer to relevant materials. The origin of the queue cannot be traced. Before there was a digital computer, there had been records of the queue in Mathematics Applications. There are also many examples of queues in our lives, such as queuing for tickets, or queuing for phone numbers during meals, or calling bank customer service to select manual services, each time, we will be prompted that "the customer service staff is busy, please be patient." because there are too few customer service personnel, the customer who calls the phone needs to wait in the time sequence. Here, I would like to praise KFC's home delivery service, which is not suspected of advertising. It can be accessed once a dozen times without waiting. However, every time I call a bank's customer service (which bank won't be called), I usually wait for a long time and always tell me that "the bank is being transferred. Please wait ", after three clicks, it becomes "the customer service is busy. Please be patient!" Then let me play a difficult song. Who is the boss of the money ...... It's not easy to repost the code.

[Monday algorithm] decrypts the QQ number-queue
Http://bbs.ahalei.com/thread-4489-1-1.html
(Source: Aha _ Programming starts from here)

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.