Queue--Decrypt QQ number

Source: Internet
Author: User

The new semester began, small ha is a small hum of the new deskmate (small ha is a little beauty oh ~), small hum to small HA inquired QQ number, small Kazakhstan of course not directly tell Xiao hum, reason why you understand. So Xiao Ha gave a 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, and then put the 2nd number at the end of the string, then delete the 3rd number and then the 4th number to the end of the string, and then delete the number of 5th ... Until the last number is left, the last number is also deleted. In accordance with the order just removed, the number of these deleted is a small ha qq. Now you come and help little hum. Xiao Ha gives little hum. The number of encrypted strings is "6 3 1 7 5 8 9 2 4".

OK, now it's your turn to do it. Go to find 9 notes or small pieces of paper, "6 3 1 75 8 9 2 4" These 9 numbers are written on 9 notes respectively, simulating the decryption process. If you do not understand the wrong decryption rules, after the decryption of Xiao Ha QQ number should be "6 1 5 9 4 7 2 8 3".

        Actually the process of decryption is like "queuing up" these numbers. Take two from the front, the 1th one, and the 2nd one to the tail. The process is like this: Just beginning this string is "6 3 1 7 5 8 9 2 4", first delete 6 and put 3 at the end of this number of strings, updated to "1 7 5 8 9 2 4 3". Next delete 1 and put 7 at the end, which is updated to "5 8 9 2 4 3 7". Delete 5 and put 8 at the end of "9 2 4 3 7 8", delete 4 and put 3 at the end that is "7 8 2 3", delete 7 and put 8 at the end of "2 3 8", delete 2 and put 3 at the end, "8 3", delete 8 and put 3 at the end of "3", and finally delete 3. The order of the deletions is therefore "6 1 5 9 4 7 2 8 3", this is the QQ number of Xiao Ha, you can add her to try ^_^.

Now that you have figured out the decryption rules, you might as well try to program yourself, and I'm sure you'll be able to write it out.

First, an array is required to store this number of strings, which is intq[101]. and initialize this array as intq[101]={0,6,3,1,7,5,8,9,2,4}; (Here I write more than a 0 to fill the q[0], because I prefer to start with the q[1], array initialization is not very understanding students can go to see my last book, AHA C! Think fast you step) next is the process of simulating decryption.

The first step in decryption is to delete the first number, and you can think about how to delete a number in the array. The simplest way is to move all the subsequent numbers to the front, overwriting the previous number. Just like we are in line to buy tickets, the people in front of the first buy to leave, all the people need to take a step forward to make up for the previous vacancy, but such a practice is time-consuming.

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

Now there are 9 numbers, and 9 numbers are all put into the queue after head=1;tail=10; the number between head and tail is the number of "active" in the current queue. If you want to delete a number, the head++ will be OK, so you can still keep the number between head and tail as "active" in the current queue. It's a cost-effective way to waste a space and save a lot of time. Add a new number is also very simple, put the number of need to add to the end of the team Q[tail] and then tail++ Ouke.

Let us summarize, in the team first delete a number of operation is head++;

The operation of adding a number at the end of the team (assuming this number is X) is q[tail]=x;tail++; The entire decryption process, please see below this domineering external leakage figure. The final output is 6 1 5 94 7 2 8 3, the code is implemented as follows.
#include <stdio.h>intMain () {intq[102]={0,6,3,1,7,5,8,9,2,4},head,tail; inti; //Initialize QueueHead=1; Tail=Ten;//There are already 9 elements in the queue, the last position of the tail's tail     while(Head<tail)//Loop is executed when the queue is not empty    {        //Print the team first and the team first out of the teamprintf"%d", Q[head]); Head++; //first add the first number of new team to the end of the teamq[tail]=Q[head]; Tail++; //and the team's first team .head++;    } getchar (); GetChar (); return 0;}
How did the above code run successfully? Now let's summarize the concept of the queue. A queue is a special linear structure that allows the deletion of a queue's header (head) to be called "out-of-line", while inserting at the end of a queue (tail) is called "enqueued". When there are no elements in the queue (that is, head==tail), it is called an empty queue. There are many things in our daily life that are consistent with the nature of the queue. For example, we have mentioned before the ticket, each queue to buy a ticket window is a queue.       In this queue, the new people are always standing in the last side of the queue, the sooner the more people on the front will be able to buy tickets, that is, the first person to serve first, we call the "first Infirst Out,fifo" principle. The queue will be the core data structure for our future learning breadth-first search and the Bellman-ford shortest-path algorithm for queue optimization. So now the three basic elements of the queue (an array, two variables) are encapsulated as a struct type, as follows.

struct queue{    int data[];   the body of the queue, used to store the contents    int head;   team head    int tail;   team Tail };
Above we define a struct type, we usually place it outside the main function, notice that there is a number at the end of the definition of the struct. struct is the key word of struct, and queue is the name we have for this struct. The structure has three members: integer array data, integer head, and integral type tail. So that we can put these three pieces together as a whole to treat.       You can understand this: we define a new type of data, which is very powerful, and each variable defined with this new type can store an integer array and two integers at the same time. With the new struct type, how do you define struct variables? Quite simply, this is the same way we defined variables before, as follows.
struct queue q;
Please note that the struct queue needs to be used as a whole and cannot write queue Q directly, so we define a struct variable Q. This struct variable q can satisfy all the operations of the queue. So how do you access the internal members of a struct variable? You can use the. Number, 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 is a queue operation implemented using structs.
#include <stdio.h>structqueue{intdata[ -];//the body of the queue, used to store the content    intHead//Team First    intTail//End of Team}; intMain () {structqueue q; inti; //Initialize QueueQ.head=1; Q.tail=1;  for(i=1; i<=9; i++)    {        //Insert 9 numbers into a queue in turnscanf"%d",&Q.data[q.tail]); Q.tail++; }                                                  while(Q.head<q.tail)//Loop is executed when the queue is not empty    {        //Print the team first and the team first out of the teamprintf"%d", Q.data[q.head]); Q.head++; //first add the first number of new team to the end of the teamq.data[q.tail]=Q.data[q.head]; Q.tail++; //and the team's first team .q.head++;    } getchar (); GetChar (); return 0;}

The above notation seems redundant, but it can strengthen your understanding of the algorithm in the queue. C + + STL library already has the implementation of the queue, interested students can see the relevant materials. The origin of the queue has not been traced. There is already a record of the queue in the math application before the digital computer is available. We live in the queue of examples are also abound, such as queuing to buy tickets, or meal time used to queue waiting for the station-to-station, or call the Bank customer service to choose Human Services, each time will be prompted "customer service staff is busy, please wait patiently", because the customer service staff are too few, call customers need to follow the time to enter Wait in order and so on. Here to praise the KFC's home emergency delivery, no advertising suspicion ah, every time a dozen on the pass, basically do not need to wait. But every time I call the bank's customer service (specifically which bank is not named) basically have to wait a long time, always tell me "is the transfer, please later", beep Beep after three to become "customer service is busy, please be patient!" "Then give me a piece of music that's hard to listen to." It seems that the money in who is the boss ah ...

Queue--Decrypt QQ number

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.