△UVA120

 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           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).

 

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

题目大意:
给你一叠薄煎饼,请你写一个程序来指出要如何翻转(flip)才能使这些薄煎饼按半径由小到大排好。所有的薄煎饼半径均不相同。
(这里为了简化问题,就不说从上到下了,就按数组下标来说)。
翻转(flip):比如输入一组数:5 1 2 3 4,数组下标分别为1,2,3,4,5。翻转(flip)只能从a[i]到a[1]进行翻转(翻转后,
会依相反的次序排列)。
5 1 2 3 4 从4到5翻转(从a[5]到a[1])
4 3 2 1 5 从1到4翻转(从a[4]到a[1])
1 2 3 4 5 从小到大排到完成

而翻转(flip),如果从最后一个数开始翻转就是flip(1),从倒数第二个数开始翻转就是flip(2)。
所以题目输出1 2 0

解题思路:
因为翻转不是随便指定两个数进行翻转,而是必须
从a[i]到a[1]进行翻转,所以应该先找出当前最大的数放在它应该在的位置。
例如,输入一组数:
3 1 2 5 4
当前最大的数为5,它应该在下标为5的位置上,所以将它翻转到a[1]
5 2 1 3 4
然后翻转到a[5]的位置上
4 3 1 2 5
此时已经排好5,只看前4个数,所以长度-1
同理,找前4个数中最大的数是4。4已经在a[1]位置,所以将它翻转到a[4]
2 1 3 4 5
长度-1,3已经排好
,长度-1,2已经在a[1]位置,所以将它翻转到a[2]

1 2 3 4 5
所以输出:
2 1 2 4 0

 1 #include<cstdio>
 2 
 3 #define maxn1 35
 4 #define maxn2 1005
 5 
 6 int a[maxn1];
 7 int flip[maxn2];
 8 
 9 int maxNum(int m)//当前最大数的下标
10 {
11     int i,max=m;
12     for(i=1;i<m;i++)
13     {
14         if(a[i]>a[max])
15             max=i;
16     }
17     return max;
18 }
19 
20 void Turn(int m)//从a[i]到a[m]的数进行翻转
21 {
22     int i,t;
23     for(i=1;i<=m/2;i++)
24     {
25         t=a[i];
26         a[i]=a[m-i+1];
27         a[m-i+1]=t;
28     }
29 }
30 
31 int main()
32 {
33     int n,i,j,pos,max;
34     char c;
35     while(scanf("%d%c",&a[1],&c) != EOF)
36     {
37         if(c=='
')
38         {
39             printf("%d
",a[1]);
40             printf("0
");
41             continue;
42         }
43         i=2;
44         while(1)
45         {
46             scanf("%d%c",&a[i],&c);
47             if(c=='
')
48                 break;
49             i++;
50         }
51         n=i;
52         for(i=1;i<n;i++)
53         {
54             printf("%d ",a[i]);
55         }
56         printf("%d
",a[n]);
57         pos=n;//pos为当前长度
58         j=0;
59         while(pos>=1)
60         {
61             max=maxNum(pos);//找出当前最大数的下标
62             if(max<pos)//如果当前最大数的下标不在当前的最后一个位置(它应在的位置)
63             {
64                 Turn(max);//则从最大数的下标到a[1]进行翻转,其实是把它翻转到a[1](注意:如果此时最大数就在a[1],则不进行翻转)
65                 if(max!=1)
66                 {
67                     flip[++j]=n-max+1;//所以如果最大数不在a[1],则进行了翻转,要记下此时的flip[j]
68                 }
69                 Turn(pos);//将在a[1]的最大数,翻转到它应在的位置
70                 
71                 flip[++j]=n-pos+1;//记下此时的flip[j]
72             }
73             pos--;//每次把最大的一个数排好后,当前长度-1,只看前面的几个数,找到其中最大的数。即当前最大的数
74         }
75         for(i=1;i<=j;i++)
76         {
77             printf("%d ",flip[i]);
78         }
79         printf("0
");
80     }
81     return 0;
82 }


 
原文地址:https://www.cnblogs.com/youdiankun/p/3683365.html