UVa OJ 120
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 languages.
棧和隊列常常被視為數(shù)據(jù)結(jié)構(gòu)中的面包和黃油,廣泛應(yīng)用在體系結(jié)構(gòu)、分析、操作系統(tǒng)和離散事件等領(lǐng)域。棧同時也在形式語言理論中發(fā)揮著重要的作用。
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.
這個問題是讓一個挑剔的廚師按照獨特而完備的一組規(guī)則翻動煎餅,以保持煎餅(而不是面包)中的黃油和營養(yǎng)素不被燒壞。(這句話實在不知道怎么翻譯才好,還望各位老師指正!Ps. 這道題翻譯的難度要比解題大多了!!)
?
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 "flips". 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 stack of n pancakes has position n.
為煎餅疊排序是通過一些列的“翻轉(zhuǎn)”動作來完成的。一個翻轉(zhuǎn)動作就是將一個小鏟插到煎餅疊中的某兩個煎餅之間,然后將小鏟上面的所有煎餅翻轉(zhuǎn)(倒轉(zhuǎn)小鏟上面的子棧)。每個翻轉(zhuǎn)動作由其開始的位置給出,即小鏟上面子棧中最底下一個煎餅的編號。整疊煎餅中最下面一個的位置為1,n個煎餅的疊中最上面一個的位置為n。
A stack is specified by giving the diameter of each pancake in the stack in the order in which the pancakes appear.
一個煎餅疊由一組表示其中各煎餅直徑的數(shù)構(gòu)成,它們排列的順序就是給出的這些數(shù)的順序。
For example, consider the three stacks of pancakes below (in which pancake 8 is the top-most pancake of the left stack):
比如下面三個煎餅疊(煎餅8是左邊一疊的最上面的一個)
???????? 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 via flip(3). The middle stack can be transformed into the right stack via the command flip(1).
左邊一疊可以通過翻轉(zhuǎn)第3個煎餅變成中間一疊的順序。中間一疊可以通過翻轉(zhuǎn)第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.
輸入包括一系列煎餅疊。每疊都由1到30個煎餅組成,并且每個煎餅的直徑都在 1到100之間。輸入由EOF結(jié)束。每疊煎餅獨占一行,最上面的在行首,最下面的在行尾,各煎餅中間由空格隔開。
?
The Output
輸出
For each stack of pancakes, the output should 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 should be terminated by a 0 (indicating no more flips necessary). Once a stack is sorted, no more flips should be made.
對應(yīng)于每疊煎餅數(shù)據(jù),必須在第一行輸出原疊的內(nèi)容,接下來輸出一組翻轉(zhuǎn)動作的序列,使得這一疊煎餅自底向上由大至小的排列。輸出的每一組翻轉(zhuǎn)動作序列都要由0來結(jié)束(表示不再進行翻轉(zhuǎn))。一旦一疊煎餅已經(jīng)排好序,就不能再進行任何翻轉(zhuǎn)。
?
Sample Input
輸入示例
1 2 3 4 5
5 4 3 2 1
5 1 2 3 4
?
Sample Output
輸出示例
1 2 3 4 5
0
5 4 3 2 1
1 0
5 1 2 3 4
1 2 0
?
轉(zhuǎn)載于:https://www.cnblogs.com/Alandre/p/3425091.html
總結(jié)
以上是生活随笔為你收集整理的UVa OJ 120的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序员,你也该为自己想想未来
- 下一篇: java中保留两位小数(四舍五入后)