zoukankan      html  css  js  c++  java
  • Uva 120 Stacks of Flapjacks

    Stacks of Flapjacks 

    Time limit: 3.000 seconds

    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


    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    int stack[32];
    
    int swap(int pos, int n)
    {//抛从pos位置到栈顶的元素 
        int i, j, temp;
        for(i=pos,j=n-1; i<j; ++i, --j)
        {
            temp = stack[i];
            stack[i] = stack[j];
            stack[j] = temp;
        }
        return 0;
    }
    int main()
    {
        int i, j, n, temp, count = 0, flag = 0, base, pos, max, t, k = 0;
        char  ch;
        while(scanf("%c", &ch) != EOF)
        {
            if(ch == '\n')
            {
                stack[count++] = k;  //进栈最后一个数 
                if(!count) continue;
                for(i=0; i<count; ++i)
                if(!i)printf("%d", stack[i]);
                else printf(" %d", stack[i]);
                printf("\n");
                swap(0, count);
                
                for(t=0,base=-1; t<count; ++t)
                {
                    if(count == 1) 
                    {
                        printf("0\n"); 
                        break;
                    }
                    pos = max = 0;
                    for(i=base+1; i<count; ++i)
                    {//找到sub-stack中最大的元素(烙饼) 
                        if(max < stack[i])
                        {
                            pos = i;
                            max = stack[i];
                        }
                    }
                    if(pos != count-1 && pos != base+1)
                    {//特殊情况: 栈底排好序的烙饼上的第一烙饼刚好是未排序中最大的饼
                     //           寻找未排序中最大的烙饼时,最顶的烙饼为最大的饼
                        printf("%d ", pos+1);
                        swap(pos, count);
                    }
                    if(pos != base+1)
                    {
                        printf("%d ", base+2);
                        swap(base+1, count);
                    }
                    
                    base++;
                    if(base == count-1)
                    {
                        printf("0\n");
                        break;
                    }
                }
                
                k = 0, flag = count = 0;
            }
            else
            {
                if(isdigit(ch))
                {
                    k = k*10 + (ch - '0');  //处理输入的数 
                    flag = 1;
                }
                else if(flag)
                {
                    flag = 0; 
                    stack[count++] = k;
                    k = 0;
                }
            }
        }
        
        if(ch != '\n')
        {//遇到文件结束符时处理最后一道数据,(纯属复制粘贴,可以调用函数) 
            stack[count++] = k;
            for(i=0; i<count; ++i)
                if(!i)printf("%d", stack[i]);
                else printf(" %d", stack[i]);
                printf("\n");
                swap(0, count);
                
                for(t=0,base=-1; t<count; ++t)
                {
                    if(base == count-1) 
                    {
                        printf("0\n"); 
                        break;
                    }
                    pos = max = 0;
                    for(i=base+1; i<count; ++i)
                    {
                        if(max < stack[i])
                        {
                            pos = i;
                            max = stack[i];
                        }
                    }
                    if(pos != count-1 && pos != base+1)
                    {
                        printf("%d ", pos+1);
                        swap(pos, count);
                    }
                    if(pos != base+1)
                    {
                        printf("%d ", base+1);
                        swap(base+1, count);
                    }
                    
                    base++;
                    if(base == count-1)
                    {
                        printf("0\n");
                        break;
                    }
                }
        }
        return 0;
    }

    解题报告:

    这题看起来是一种比较有意思的排序,排序的步骤初想为是这样的:

    找到目前的最大烙饼的位置->在此处抛一次->在栈底向上找到比目前最大烙饼小的第一个烙饼的位置->抛一次->......

    Q1:

    最后一个数的输入和最后一个case的排序

    flip的特别情况:

    栈底排好序的烙饼上的第一烙饼刚好是未排序中最大的饼

    寻找未排序中最大的烙饼时,最顶的烙饼为最大的饼

    PS: 原谅我这一只一直长不大的菜鸟!!

  • 相关阅读:
    海量数据查询关系型数据库存储大数据,要点就是:简单存储、分区分表、高效索引、批量写入
    微服务架构
    多租户系统架构
    SaaS模式实现架构
    net Core 2.1新功能Generic Host(通用主机)
    深度学习与机器学习
    RabbitMQ和Kafka
    为什么使用框架
    迅雷在P2P网络中的另类上传速度
    Spring配置中的"classpath:"与"classpath*:"的区别研究(转)
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/2843464.html
Copyright © 2011-2022 走看看