4. Stacks of Flapjacks

来源:互联网 发布:淘宝的老陈胆机怎么样 编辑:程序博客网 时间:2024/05/17 22:44

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.

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 ofn pancakes has positionn.

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 commandflip(1).

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

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.

测试输入关于“测试输入”的帮助期待的输出关于“期待的输出”的帮助时间限制关于“时间限制”的帮助内存限制关于“内存限制”的帮助额外进程关于“{$a} 个额外进程”的帮助测试用例 1以文本方式显示

  1. 1 2 3 4 5↵
  2. 5 4 3 2 1↵
  3. 5 1 2 3 4↵
以文本方式显示
  1. 1 2 3 4 5↵
  2. 0↵
  3. 5 4 3 2 1↵
  4. 1 0↵
  5. 5 1 2 3 4↵
  6. 1 2 0↵
1秒1024KB0


//在0-last找最值,倒到0(start)【如果最值不在0的话】,再倒到last,last--,再重复这一过程,直到满足一个排序#include<cstdio>//ACint num[102] = { 0 }, numm[102] = { 0 };int judge(int num[],int len){for (int i = 0; i < len-1; i++)if (num[i] <= num[i + 1])continue;elsereturn 1;return 0;}int FindMax(int num[], int List){int Max = 0;for (int i = 1; i <= List; i++)if (num[i] > num[Max])Max = i;return Max;}void SwapNum1(int num[],int List,int MaxZ){int sswap;for (int i = 0, j = MaxZ; j > i; i++, j--){sswap = num[i];num[i] = num[j];num[j] = sswap;}for (int i = 0, j = List; j > i; i++, j--){sswap = num[i];num[i] = num[j];num[j] = sswap;}}void SwapNum2(int num[], int List){int sswap;for (int i = 0, j = List; j > i; i++, j--){sswap = num[i];num[i] = num[j];num[j] = sswap;}}int main(){int st ,count,List,MaxZ,k;//st统计输入数(比真实大1),count翻转次数,List输入数组的最右边的数在数组中的位置,MaxZ输入数组最大数在数组中的位置,k用来判断输入是否为EOF了while (1){st = 0; count = 0;//用来输入数字流,这个可以当作一个不错的范例for (int i = 0;; i++){k=scanf("%d", &num[i]);if (k == EOF)//注意只有一个输入的情况!!!!{k = 200;break;}st++; //计算输入个数 if (getchar() == '\n')break; //遇回车中断}if (k == 200)break;for (int i = 0; i < st; i++)//输出饼子 if (i < st - 1)printf("%d ", num[i]);elseprintf("%d\n",num[i]);List = st-1;while (judge(num,st)){MaxZ = FindMax(num, List);//在num[0]到num[List]间的最大数,并传回位置if (MaxZ != List)//如果最大数的位置不在List,则进行翻转{if (MaxZ != 0)//最大数位置既不在List,也不在0,则先翻到0,再翻到List{SwapNum1(num, List, MaxZ);numm[count++] = st - MaxZ;numm[count++] = st - List;}else//如果最大数位置在0,则直接翻转到List{SwapNum2(num, List);numm[count++] = st - List;}}List--;}for (int i = 0; i <count; i++)//输出所翻层数 printf("%d ", numm[i]);printf("0\n");}return 0;}





原创粉丝点击