Background
Stacks and queues are often considered the bread and butter of data structures and find use in architecture, parsing, operating systems, and discrete event simulation. stacks are also important in the theory of formal versions.
This problem involves both butter and sustenance in the form of pancakes rather than bread in addition to a finicky server who flips pancakes according to a unique, but complete set of rules.
The Problem
Given a stack of pancakes, you are to write a program that indicates how the stack can be sorted so that the largest pancake is on the bottom and the smallest pancake is on the top. the size of a pancake is given by the Pancake's diameter. all pancakes in a stack have different diameters.
Sorting a stack is done by a sequence of pancake ''flip ''. A Flip consists of inserting a spatula between two pancakes in a stack and flipping (reversing) the pancakes on the spatula (reversing the sub-stack ). A Flip is specified by giving the position of the pancake on the bottom of the sub-stack to be flipped (relative to the whole stack ). the pancake on the bottom of the whole stack has position 1 and the pancake on the top of a stackNPancakes has positionN.
A stack is specified by giving the diameter of each pancake in the stack in the order in which the pancakes appear.
For example, consider the three stacks of pancakes below (in which pancake 8 is the top-most pancake of the Left stack ):
8 7 2 4 6 5 6 4 8 7 8 4 5 5 6 2 2 7
The stack on the Left can be transformed to the stack in the middle
Flip (3). The middle stack can be transformed into the right Stack via the command
Flip (1).
The input
The input consists of a sequence of stacks of pancakes. each stack will consist of between 1 and 30 pancakes and each pancake will have an integer diameter between 1 and 100. the input is terminated by end-of-file. each stack is given as a single line of input with the top pancake on a stack appearing first on a line, the bottom pancake appearing last, and all pancakes separated by a space.
The output
For each stack of pancakes, the output shoshould echo the original stack on one line, followed by some sequence of flips that results in the stack of pancakes being sorted so that the largest diameter pancake is on the bottom and the smallest on top. for each stack the sequence of flips shoshould be terminated by a 0 (indicating no more flips necessary ). once a stack is sorted, no more flips shoshould be made.
Sample Input
1 2 3 4 55 4 3 2 15 1 2 3 4
Sample output
1 2 3 4 505 4 3 2 11 05 1 2 3 41 2 0
# Include <cstdio> # include <iostream> # include <sstream> # include <algorithm> using namespace STD; const int maxn = 30 + 5; int n, a [maxn]; void flip (INT p) {for (INT I = 0; I <p-I; I ++) Swap (A [I], a [p-I]); printf ("% d", N-P);} int main () {string s; while (Getline (CIN, S )) {cout <S <"\ n"; stringstream SS (s); n = 0; while (SS> A [n]) n ++; cout <n <Endl; For (INT I = n-1; I> 0; I --) {// locate the location of the current maximum value; int P = max_element (, A + I + 1)-A; cout <p <Endl; // if the maximum value is the same as its location, continue to the next one; If (P = I) continue; // convert; place the maximum value in the first position; if (P> 0) Flip (p); flip (I);} printf ("0 \ n ");} return 0 ;}
Stacks of flapjacks