Challenges of csdn programming-book Transfer

Source: Internet
Author: User
Question details

Xiaoqiang has three boxes, A, B, and C, which are used to hold books. All the books (N in total) are stacked on a in the sequence from small to large, now he wants to put all the books in C. Every time he takes a book (not enough) from booka to B, a box is flipped, and B book is taken from B (not enough) to C and B box is flipped. Repeat the operation until all the books arrive at C and find the order of the books in C. For details, see the example.

Input description:

The input consists of multiple groups of data. Each group occupies one row, with three numbers. N (1 <= n <= 10 ^ 5 ), A (1 <= A <= 10 ^ 9), B (1 <= B <= 10 ^ 9). For the meaning, see the topic description. End with a file.

Output description:

Each group of data outputs a row. First, the number of data groups is output, then the number of N is output, and the arrangement of C books is made.


Answer description

Input example:

4 2 1

Output example

Case 1: 1 4 2 3



Hint

Initial status A: 4321 B: NULL C: NULL

First time: A-> B a: 21 B: 34 C: NULL

A flipped a: 12 B: 34 C: NULL

B-> c a: 12 B: 4 C: 3

B flip a: 12 B: 4 C: 3

Second time: A-> B a: null B: 214 C: 3

A flip a: null B: 214 C: 3

B-> c a: null B: 14 C: 23

B flip a: null B: 41 c: 23

Third time: B-> c a: null B: 1 C: 423

B flip a: null B: 1 C: 423

Fourth time: B-> c a: null B: NULL C: 1423

Idea: This question can be done with a two-way linked list, because a and B both have a flip operation, while a two-way linked list can traverse the elements of a table in two directions, so after the flip, it is equivalent to taking the linked list element from another direction. In the end, two-way linked lists are used to represent a and B, and a stack is used to represent C. We have been simulating the question operation. When A and B are empty, we can output the value of C in the stack.

Code:

# Include <iostream> # include <list> # include <stack> using namespace STD; int main () {int n, a, B, n_case = 0; while (CIN> N) {n_case ++; CIN> A> B; List <int> a_list; // list of elements storing a <int> B _list; // store B's element stack <int> S; // store c's element for (INT I = 1; I <= N; I ++) a_list.push_front (I ); // use 1-N to fill the aint COUNT = 1; while (! (A_list.empty () & B _list.empty () {int a_tmp = A; int B _tmp = B; If (count % 2 = 1) // obtain the element {While ((! A_list.empty () & a_tmp --) {B _list.push_front (a_list.front (); // obtain the first a element a_list.pop_front ();} while ((! B _list.empty () & B _tmp --) {S. push (B _list.front (); // obtain the first B element B _list.pop_front ();} else // obtain the element {While ((! A_list.empty () & a_tmp --) {B _list.push_back (a_list.back (); a_list.pop_back () ;}while ((! B _list.empty () & B _tmp --) {S. push (B _list.back (); B _list.pop_back () ;}count ++ ;}cout <"case" <n_case <":"; while (! S. Empty () {cout <''<S. Top (); S. Pop () ;}cout <Endl ;}return 0 ;}


Challenges of csdn programming-book Transfer

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.