Stacks of Flapjacks(翻煎饼) 蓝桥杯 每周一练

[问题描述]（老师已经为宝宝们翻译好啦）

Stacks and Queues are often considered the bread andbutter of data structures and find use in architecture, parsing,operating systems, and discrete event simulation.Stacks are also important in thetheory of formal languages.

This problem involves both butter and sustenance inthe form of pancakes rather than bread in addition to a finicky server who flips pancakesaccording to a unique, but complete set of rules.

Given a stack of pancakes, you are to write a programthat indicates how the stack can be sorted so that the largest pancake is on the bottom and thesmallest pancake is on the top.

The size of a pancake is given by the pancake’sdiameter.

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 twopancakes in a stack and flipping (reversing) all the pancakes on thespatula (reversing the sub-stack).

A flip is specified by giving the position of thepancake on the bottom of the sub-stack to be flipped (relative to the whole stack).

The pancake on the bottom of the whole stack hasposition 1

and the pancake on the top of a stack of n pancakeshas position n.

A stack is specified by giving the diameter of eachpancake in the stack in the order in which the pancakes appear.

For example, consider the three stacks of pancakesbelow (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 stackin the middle via flip(3). The middle stack can be transformed into the right stack via the commandflip(1).

Input（输入）

The input consists of a sequence of stacks ofpancakes. Each stack will consist of between 1 and 30 pancakes and each pancake will have an integerdiameter between 1 and 100. The input is terminated by end-of-file. Each stack is given as a single lineof 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.

Output（输出）

For each stack of pancakes, the output should echo theoriginal stack on one line, followed by some sequence of flips that results in the stack ofpancakes being sorted so that the largest diameter pancake is on the bottom and the smallest on top. For eachstack the sequence of flips should be terminated by a ‘0’ (indicating no more flips necessary). Once astack 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

*/

while(scanf（“%d”，&a[0])!=EOF）
{
i=1；
while(scanf("%d",&a[i])!=EOF)
{
i+=1；
}
n=i;

}

#include <stdio.h>
#include <string.h>
int len,a[35],b[100],n,k,max,temp;
char ch[35];
void zhuan()
{
n=0;
int sum=0;
len=strlen(ch);
for(int i=0;i<len;i++)
{
if(ch[i]!=' ')
{
sum=sum*10+ch[i]-'0';
}
else
{
a[n++]=sum;
sum=0;
}
if(i==len-1)
a[n++]=sum;
}
}
int main()
{
while(gets(ch)) //注意
{
zhuan();
k=0;
int ff=n;
while(ff)
{
max=a[0];
temp=0;
for(int i=0;i<ff;i++)
{
if(max<a[i])
{
temp=i;
max=a[i];
}
}
if(temp==ff-1)
{
ff--;
}
else
{
b[k++]=n-ff+1;  //注意
for(int i=temp,j=ff-1;i<=j;j--,i++)
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
ff--;
}
}
puts(ch);
//printf("\n");
for(int i=0;i<k;i++)
{
printf("%d ",b[i]);
}
printf("0\n");
}

return 0;
}


©️2019 CSDN 皮肤主题: 数字50 设计师: CSDN官方博客