120 - Stacks of Flapjacks
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.
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 ``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.
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 7The 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) .
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 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.
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
题意:
新设定了一种排序方法:基本原理类似冒泡排序,每次要将最大的饼放在栈的底部位置。实现方式是通过反转(栈顶部)到(反转位置)之间的元素。最后通过若干次反转,实现栈顶部到底部的升序,并输出每次反转位置(栈底部是1,顶部是n)
解决思路:
1. 最大的饼存在三个位置,要分情况处理
a. 饼已经位于底部对应的位置,不做处理;
b. 饼位于栈的顶部,反转一次;
c. 饼位于中间,反转二次:先将最大饼转到顶部;再转到底部对应位置。
2. 依次将当前最大的饼通过上述反转到对应的位置,即可
代码如下:
#include<stdio.h>
int stack[31];
int buf[31];
void reverse(int l,int r) //逆转stack
{
int i,j,temp;
for(i=l,j=r;i<=j;i++,j--){
temp=buf[i];buf[i]=buf[j];buf[j]=temp;
}
}
void flipsort(int n)
{
int max,max_flag;
int i,j;
for(i=n;i>=0;i--)//底部位置一次次枚举
{
max=-1; //寻找最大的值
for(j=0;j<=i;j++){
if(buf[j]>max) { max=buf[j]; max_flag=j; }
}
if(max_flag==i) continue;//饼已经在底部位置
else if(max_flag==0){ //饼在顶部位置
reverse(0,i);
printf("%d ",n+1-i);
}
else{ //饼在中间位置
reverse(0,max_flag);
printf("%d ",n+1-max_flag);
reverse(0,i);
printf("%d ",n+1-i);
}
}
printf("0\n");
}
int main()
{
int n=0;
while(scanf("%d",&stack[n])!=EOF)
{
while(getchar()!='\n'){
scanf("%d",&stack[++n]);
}
for(int i=0;i<n;i++)
printf("%d ",stack[i]);
printf("%d\n",stack[n]);
for(int i=0;i<=n;i++)
buf[i]=stack[i];
flipsort(n);
n=0;
}
return 0;
}