日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UVa OJ 120

發(fā)布時間:2024/4/17 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UVa OJ 120 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

#include <algorithm> #include <iostream> #include <deque> #include <string> #include <sstream> using namespace std; int main() {for(string strLine;getline(cin,strLine);cout << '0' << endl){cout << strLine << endl;istringstream iss(strLine);deque<int> Stack;for(int nDiam; iss >> nDiam;Stack.push_front(nDiam));for(deque<int>::iterator it = Stack.begin(); it != Stack.end();++it){deque<int>::iterator iMax = max_element(it,Stack.end());//如果最大就是it if(iMax !=it){//如果最大的不是最上面,先最大的反轉(zhuǎn)到最上面 if(iMax != Stack.end()-1){reverse(iMax,Stack.end());cout << distance(Stack.begin(),iMax) + 1 <<' ';}//翻轉(zhuǎn)最上面的位置 reverse(it,Stack.end());cout << distance(Stack.begin(),it) + 1 << ' ';}}}return 0; }

?

轉(zhuǎn)載于:https://www.cnblogs.com/Alandre/p/3425091.html

總結(jié)

以上是生活随笔為你收集整理的UVa OJ 120的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。