zoukankan      html  css  js  c++  java
  • Stacks of Flapjacks(栈)

     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



    //栈的简单应用,不难,,就是每次想办法把最大的放下去,毕竟不需要最优解
    坑的是结果要把题目输出一遍。。。
     1 #include <stdio.h>
     2 #include <iostream>
     3 using namespace std;
     4 
     5 int num[35];
     6 int op[1000];
     7 int N,times;
     8 
     9 int Read(char str[])
    10 {
    11     int i=0;
    12     while (str[i++]==' ');
    13     i--;
    14     int k=1;
    15     for (i=i;str[i];i++)
    16     {
    17         if (str[i]!=' ')
    18         {
    19             int j,res=0;
    20             for (j=i;str[j]!=' ';j++)
    21             {
    22                 if (str[j]=='') break;
    23                 res+=str[j]-'0';
    24                 res*=10;
    25             }
    26             i=j-1;
    27             num[k++]=res/10;
    28         }
    29     }
    30     return k-1;
    31 }
    32 
    33 void Ni(int x)
    34 {
    35     int i=1,j=x;
    36     while (1)
    37     {
    38         swap(num[i],num[j]);
    39         if (i<j) i++;
    40         if (j>i) j--;
    41         if (i==j)break;
    42     }
    43 }
    44 
    45 void Func()
    46 {
    47     int n=N;
    48     while (n)
    49     {
    50         int x=1,mmm=num[1];
    51         for (int i=1;i<=n;i++)
    52         {
    53             if (num[i]>mmm)
    54             {
    55                 mmm=num[i];
    56                 x=i;
    57             }
    58         }
    59         if (x!=n)//最大的不在n的位置
    60         {
    61             if (x==1)
    62             {
    63                 Ni(n);
    64                 op[times++]=N-n+1;
    65             }
    66             else
    67             {
    68                 Ni(x);
    69                 op[times++]=N-x+1;
    70                 Ni(n);
    71                 op[times++]=N-n+1;
    72             }
    73         }
    74         n--;
    75     }
    76 }
    77 
    78 int main()
    79 {
    80     char strnum[200];
    81     while (gets(strnum))
    82     {
    83         N=Read(strnum);//读数
    84         times=0;
    85         Func();//不断将最大的放到最下面去
    86         printf("%s
    ",strnum);
    87         for (int i=0;i<times;i++)
    88         printf("%d ",op[i]);
    89         printf("0
    ");
    90     }
    91     return 0;
    92 }
    View Code



  • 相关阅读:
    DelphiXE下的字符串变化
    FastMM配置文件详解
    [转] FastMM、FastCode、FastMove的使用
    FastMM 定位内存泄露的代码位置
    转 Delphi中使用FastMM4结合View CPU避免内存泄漏
    [转] FastMM使用详解
    salesforce 零基础开发入门学习(九)Approval Process 介绍
    salesforce 零基础开发入门学习(八)数据分页简单制作
    salesforce 零基础开发入门学习(七)PickList的value值获取
    salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/6185063.html
Copyright © 2011-2022 走看看