120 - Stacks of Flapjacks

来源:互联网 发布:人工智能 招聘 深圳 编辑:程序博客网 时间:2024/05/29 05:09

Background

Stacks and Queuesare often considered the bread and butter of data structures and find use inarchitecture, parsing, operating systems, and discrete event simulation. Stacksare also important in the theory of formal languages.

This problem involvesboth butter and sustenance in the form of pancakes rather than bread inaddition to a finicky server who flips pancakes according to a unique, butcomplete set of rules.

The Problem

Given a stack ofpancakes, you are to write a program that indicates how the stack can be sortedso that the largest pancake is on the bottom and the smallest pancake is on thetop. The size of a pancake is given by the pancake's diameter. All pancakes ina stack have different diameters.

Sorting a stack isdone by a sequence of pancake ``flips''. A flip consists of inserting a spatulabetween two pancakes in a stack and flipping (reversing) the pancakes on thespatula (reversing the sub-stack). A flip is specified by giving the positionof the pancake on the bottom of the sub-stack to be flipped (relative to thewhole stack). The pancake on the bottom of the whole stack has position 1 andthe pancake on the top of a stack of n pancakes hasposition n.

A stack isspecified by giving the diameter of each pancake in the stack in the order inwhich the pancakes appear.

For example,consider the three stacks of pancakes below (in which pancake 8 is the top-mostpancake 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 theleft can be transformed to the stack in the middle via flip(3). Themiddle stack can be transformed into the right stack via the command flip(1).

The Input

The input consistsof a sequence of stacks of pancakes. Each stack will consist of between 1 and30 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 ofinput with the top pancake on a stack appearing first on a line, the bottompancake appearing last, and all pancakes separated by a space.

The Output

For each stack ofpancakes, the output should echo the original stack on one line, followed bysome sequence of flips that results in the stack of pancakes being sorted sothat the largest diameter pancake is on the bottom and the smallest on top. Foreach stack the sequence of flips should be terminated by a 0 (indicating nomore 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——30个),每个数字的大小:1——100之间,保证输入的数字各不相等,为了得到从小到大的排列,采取从头至某处反转的操作,每次操作后输出操作处的下标(从后向前:最后一个数字的下标是1)。

思路:

按照“选择排序”的思想,以从大到小的顺序将每个数字排到正确的位置。具体为:现将当前未排序的最大的数字翻转到开头处,然后再翻转到正确的位置。由于按照从大到小的顺序,当处理第I大的数字时,不会影响比它大的数字,因为这些数字都已经反转到了正确的位置。

代码:

#include<iostream>

#include<sstream>

#include<algorithm>

using namespacestd;

 

int n1[35],n2[35];//n2用来存储正确排序的结果

int n;

 

void flip(intindex);

 

int main()

{

    string s;

    while(getline(cin, s))

    {

        cout << s <<"\n";

        stringstream ss(s);

        n = 0;

        while(ss >> n1[n])//注意不能写成while(ss >> n1[n++]);

        {

            n++;

        }

        for(int i=0;i<n;i++)

        {

            n2[i]=n1[i];

        }

        sort(n2,n2+n);

        flip(n-1);

        cout<<0<<endl;

    }

    return 0;

}

 

void flip(intindex)

{

    int num;

    if(index>=0)//防止越界

    {

        num=n2[index];

        if(num==n1[index])

        {

            flip(index-1);

        }

        else

        {

            int i=-1;

            while(n1[++i]!=num);

            if(i!=0)

            {

                cout<<n-i<<"";

                for(int j=0;j<(i+1)/2;j++)

                {

                    swap(n1[j],n1[i-j]);

                }

            }

            cout<<n-index<<"";

            for(int j=0;j<(index+1)/2;j++)

            {

                swap(n1[j],n1[index-j]);

            }

            flip(index-1);

        }

    }

}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果六手机很卡网络不给力怎么办 堡垒之夜卡在载入界面怎么办 登录新福建一直说网络不给力怎么办 开发游戏平台给了钱不给东西怎么办 代号英雄与服务器断开连接了怎么办 千牛聊天页面买家信息不显示怎么办 秒拍存草稿箱的视频没了怎么办? 登录山东掌厅出现服务器错误怎么办 微信号被多人投诉被限制登录怎么办 联想平板电脑开机密码忘记了怎么办 申请的qq没登录忘了账号怎么办 炫舞时代由于网络原因登不上怎么办 qq申请太多进不了热聊怎么办 手机号申请的微信号被盗了怎么办 买菜别人少找了钱不还怎么办 在掌上英雄联盟买皮肤买错区怎么办 win8我的电脑图标没了怎么办 英雄联盟老是卡在安全扫描怎么办 英雄联盟活动送皮肤没送怎么办 电脑换完系统有些页面打不开怎么办 王卡助手交手机费页面打不开怎么办 在浏览器上打不开路由器页面怎么办 英雄联盟读条的时候自动关机怎么办 手机的位置信息开不了怎么办呢 滴滴车主接到乘客返回路程要怎么办 移动换话费积分是发什么短信怎么办 手机店积分换手机被贷款怎么办 心悦俱乐部礼包已过期是怎么办 心悦兑换的东西不是账号绑定怎么办 心悦会员绑定的手机号不用了怎么办 想在京东商城开个网店怎么办呢 京东买了东西收货了不想要了怎么办 京东转卖的商品有问题怎么办 如果衣服下架了然后有退货怎么办 想买二手车可没有懂车的人怎么办 买车的时候异地车牌回家怎么办 天猫下单显示下单人数太多券怎么办 英雄联盟进入游戏后无限崩溃怎么办 打开电视显示百度影棒打不开怎么办 家里路由器网速一会快一会慢怎么办 用快看影视下载电影网速太慢怎么办