Algorithm competition entry classic 6.1 stack and queue-card games, algorithm competition entry classic

Source: Internet
Author: User

Algorithm competition entry classic 6.1 stack and queue-card games, algorithm competition entry classic

There is a stack of cards on the table, numbered from 1 to 1 from top to bottom from the first card (that is, the top card ~ N; when there are at least two cards left, do the following: Drop the first card, and put the new one at the end of the stacked card. Input n to output the cards that are discarded each time.
Sample input:
7
Sample output:
1 3 5 7 4 2 6

1 # include <stdio. h> 2 int queue [500]; 3 int main () 4 {5 int I, n, front, rear; // n <= strlen (queue) /2 6 scanf ("% d", & n); 7 for (I = 0; I <n; I ++) 8 queue [I] = I + 1; // initialize queue 9 front = 0; // position of the first element of the Team 10 rear = n; // position of the last element of the team end 11 while (front <rear) // when the queue is not empty 12 {13 printf ("% d", queue [front ++]); // output and discard the first element of the Team 14 queue [rear ++] = queue [front ++]; // transfer the first element to the end of the team 15} 16 return 0; 17}View Code

Analysis:
1. queue: two queues are obtained from the queue header each time, and the second one is placed at the end of the queue. This data structure is called a queue or a FIFO table. FIFO indicates First In, First Out.
2. Use an array queue to implement this queue, and then set two pointers, front and rear. Although the running result is correct, if the rear value is printed at the end, the rear will be larger than n (exactly 2n ). In other words, after the program is running, queue [rear ++] = queue [front ++] reads and writes illegal memory (invalid read/write memory may not necessarily cause program crash )! Therefore, you can either increase the size of the array space or use a technology called cyclic queue to reuse the space occupied by the elements of the outbound queue.

C ++ provides a simpler processing method-STL queue:

1 # include <cstdio> 2 # include <queue> 3 using namespace std; 4 queue <int> q; 5 int main () 6 {7 int I, n; 8 scanf ("% d", & n); 9 for (I = 0; I <n; I ++) 10 q. push (I + 1); // initialize the queue 11 while (! Q. empty () // when the queue is not empty 12 {13 printf ("% d", q. front (); // print the first element of the Team 14 q. pop (); // discard the first element of the team 15 q. push (q. front (); // Add the new first element to the queue 16 q. pop (); // discard the first element of the Team 17} 18 return 0; 19}View Code

Analysis:
1. the code of this program is not much concise, but its readability is greatly enhanced: on the one hand, it is reflected in the name of the "queue" and "rear, on the other hand, it is embodied in the standard of STL Library (which is an integral part of C ++ ).
2. majic number, that is, you do not need to know the size of n in advance. Reducing the number of variables (using less than two variables front and rear) improves code readability, an important means to reduce the possibility of errors.

Related Article

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.