Background
Stacks and Queues are often considered the bread and butter of datastructures and find use in architecture, parsing, operating systems, anddiscrete event simulation. Stacks are also important in the theory offormal languages.
This problem involves both butter and sustenance in the form of pancakesrather than bread in addition to a finicky server who flips pancakesaccording to a unique, but complete set of rules.
The ProblemGiven a stack of pancakes, you are to write a program that indicates howthe stack can be sorted so that the largest pancake is on the bottomand the smallest pancake is on the top. The size of a pancake is givenby the pancake's diameter. All pancakes in a stack have differentdiameters.
Sorting a stack is done by a sequence of pancake ``flips''. A flipconsists of inserting a spatula between two pancakes in a stack andflipping (reversing) the pancakes on the spatula (reversing the sub-stack). Aflip is specified by giving the position of the pancake on the bottom ofthe sub-stack to be flipped (relative to the whole stack). The pancakeon the bottom of the whole stack has position 1 and the pancake on thetop of a stack of n pancakes has position n.
A stack is specified by giving the diameter of each pancake in thestack 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 7Input
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 rightstack via the command flip(1)ut
The input consists of a sequence of stacks of pancakes. Each stack willconsist of between 1 and 30 pancakes and each pancake will have aninteger diameter between 1 and 100. The input is terminated byend-of-file. Each stack is given as a single line of input with the toppancake on a stack appearing first on a line, the bottom pancakeappearing last, and all pancakes separated by a space.
Output
For each stack of pancakes, the output should echo the original stack onone line, followed by some sequence of flips that results in the stackof pancakes being sorted so that the largest diameter pancake is on thebottom and the smallest on top. For each stack the sequence of flipsshould be terminated by a 0 (indicating no more flips necessary). Oncea stack is sorted, no more flips should be made.
Sample Input1 2 3 4 5 5 4 3 2 1 5 1 2 3 4Sample output
1 2 3 4 5 0 5 4 3 2 1 1 0 5 1 2 3 4 1 2 0
解题思路:
对该序列找出半径最大的煎饼r[n](n为位置),r[n]如果在最底部则不需要进行翻转操作,假如不是在最底部的话,则需要先把它翻转到最上面,再翻转到最下面,则为翻转的下标一次为:N(饼的数量)- n,1
然后再对接下来的N-1个饼进行上述操作,直到翻转完成!
附上代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int r[100];//煎饼的半径
int n;
void operation(int position);//对煎饼堆进行翻转操作
int main()
{
int i,length,max,rec;
char nouse;//读取空格或者换行符
while(scanf("%d",&r[0])!=EOF)
{
nouse=getchar();
if(nouse=='\n')
{
printf("%d\n",r[0]);
printf("0\n");
continue;
}//一开始少了这一部分,老是WA,需要考虑当输入只有一个的时候
for(i=1;;i++)
{
scanf("%d",&r[i]);
nouse=getchar();
if(nouse=='\n')
break;
}
length=i+1;
n=i+1;
for(i=0;i<length;i++)
{
if(i==length-1)
printf("%d\n",r[i]);
else
printf("%d ",r[i]);
}
while(length)//循环对该子序列最大的煎饼翻转到最下面
{
max=0;
for(int j=0;j<length;j++)
if(r[j]>max)
{
max=r[j];
rec=j;
}
if(rec!=length-1)
{
if(rec!=0)
operation(rec);
operation(length-1);
}
length--;
}
printf("0\n");
}
}
void operation(int position)
{
printf("%d ",n-position);
int temp;
for(int i=0;i<=position/2;i++)
{
temp=r[i];
r[i]=r[position-i];
r[position-i]=temp;
}
}