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 //栈的简单应用,不难,,就是每次想办法把最大的放下去,毕竟不需要最优解
坑的是结果要把题目输出一遍。。。
#include <stdio.h>
#include <iostream>
using namespace std; int num[];
int op[];
int N,times; int Read(char str[])
{
int i=;
while (str[i++]==' ');
i--;
int k=;
for (i=i;str[i];i++)
{
if (str[i]!=' ')
{
int j,res=;
for (j=i;str[j]!=' ';j++)
{
if (str[j]=='\0') break;
res+=str[j]-'';
res*=;
}
i=j-;
num[k++]=res/;
}
}
return k-;
} void Ni(int x)
{
int i=,j=x;
while ()
{
swap(num[i],num[j]);
if (i<j) i++;
if (j>i) j--;
if (i==j)break;
}
} void Func()
{
int n=N;
while (n)
{
int x=,mmm=num[];
for (int i=;i<=n;i++)
{
if (num[i]>mmm)
{
mmm=num[i];
x=i;
}
}
if (x!=n)//最大的不在n的位置
{
if (x==)
{
Ni(n);
op[times++]=N-n+;
}
else
{
Ni(x);
op[times++]=N-x+;
Ni(n);
op[times++]=N-n+;
}
}
n--;
}
} int main()
{
char strnum[];
while (gets(strnum))
{
N=Read(strnum);//读数
times=;
Func();//不断将最大的放到最下面去
printf("%s\n",strnum);
for (int i=;i<times;i++)
printf("%d ",op[i]);
printf("0\n");
}
return ;
}
Stacks of Flapjacks(栈)的更多相关文章
- UVA Stacks of Flapjacks 栈排序
题意:给一个整数序列,输出每次反转的位置,输出0代表排序完成.给一个序列1 2 3 4 5,这5就是栈底,1是顶,底到顶的位置是从1~5,每次反转是指从左数第i个位置,将其及其左边所有的数字都反转,假 ...
- uva 120 stacks of flapjacks ——yhx
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data ...
- HDU 5818 Joint Stacks(联合栈)
HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- UVaOJ 120 - Stacks of Flapjacks
120 - Stacks of Flapjacks 题目看了半天......英语啊!!! 好久没做题...循环输入数字都搞了半天...罪过啊!!! 还是C方便一点...其实C++应该更方便的...C+ ...
- Uva 120 - Stacks of Flapjacks(构造法)
UVA - 120 Stacks of Flapjacks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &a ...
- uva Stacks of Flapjacks
Stacks of Flapjacks 题目链接:Click Here~ 题目描写叙述: ...
- 【思维】Stacks of Flapjacks
[UVa120] Stacks of Flapjacks 算法入门经典第8章8-1 (P236) 题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列. 试题分析:从插入排序即可找到 ...
- Stacks of Flapjacks
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data s ...
- [CareerCup] 3.3 Set of Stacks 多个栈
3.3 Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in ...
随机推荐
- JAVA之方法的重载
package com.test; //方法重载(overload)定义://1.方法名称相同//2.方法的参数类型.个数.顺序至少有一项不同//3.方法的返回类型可以不同//4.方法的修饰符可以不同 ...
- Apache环境下搭建KodExplorer网盘
Apache环境下搭建KodExplorer网盘 环境说明: 系统版本 CentOS 6.9 x86_64 软件版本 yum安装httpd和php kodexplorer4.25 1 ...
- Android Gradle基础实践
1,gradle是全新的一种IDE编程环境,Android Studio集成了Gradle IDE 2,要下载gradle(比方gradle-2.10)解压.配置环境变量.比方G:\Program ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
- 最新iOS发布App Store详细图文教程~
网上有很多关于iOS发布上架的教程,但大多比较旧而且不完整.不够清晰.所以整理了一个详细完整的iOS APP发布上架App Store的图文教程.分享给小白到大神路上前进的你我. 上架iOS需要一个苹 ...
- Android学习(十六) 通过GestureDetector进行手势识别
一.手势交互过程: 1)触屏时,触发MotionEvent事件. 2)被OnTouchListener监听,在onTouch()中获得MotionEvent对象. 3)GestureDetector转 ...
- RabbitMQ三----'任务分发 '
当有Consumer需要大量的运算时,RabbitMQ Server需要一定的分发机制来balance每个Consumer的load.试想一下,对于web application来说,在一个很多的HT ...
- 关于XSuperMES项目使用的PDF框架
我在曾经的项目中使用的是pdfbox.在读取中文文档时能够读出大部分的文字,可是在数字.分页等地方还是不可避免的出现乱码. 于是我在网上搜索,看有没有什么解决方法.看到有说法: "PDFBo ...
- ”ftp使用dos命令“
ftp不能使用dos命令,ftp有专用的命令. 在批处理文件中,如果用到dos命令获取信息(比如:系统日期),将用获取的信息,输出到ftp脚本文件中,然后执行ftp脚本文件. set yyyy=%DA ...
- NSTimer注意内存泄露(真该死)
NSTimer可以用来执行一些定时任务,比较常用的方法就是: + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTar ...