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 ...
随机推荐
- asp.net使用母版页以及Jquery和prototype要注意的问题
在母版页中引用了js,css或者其他外部文件之后,子页面就不必再重新引用,否则可能出错 prototype.js和jquery.js冲突的解决方案: <script type="tex ...
- SSO单点登录系列6:cas单点登录防止登出退出后刷新后退ticket失效报500错
这个问题之前就发现过,最近有几个哥们一直在问我这个怎么搞,我手上在做另一个项目,cas就暂时搁浅了几周.现在我们来一起改一下你的应用(client2/3)的web.xml来解决这个2b问题,首先看下错 ...
- 在Hadoop监控页面怎样查看Hive的完整SQL
如图.这里仅仅能看到简单的一段SQL.差点儿看不出详细在运行什么任务. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGFvemhhb2t1bg==/fo ...
- OkHttpClient简单封装
一.接口 public interface HttpListener { void onFinish(String reponse); void onError(Exception e); } 二.O ...
- Ant Design Mobile 使用 rc-form
引入: import { createForm } from 'rc-form'; 步骤一:绑定 form // 将form表单的api绑定到props,便于调用 const EditHeaderWr ...
- Linux如何查看进程、杀死进程、查看端口等常用命令
查看进程号 1.ps 命令用于查看当前正在运行的进程.grep 是搜索 例如: ps -ef | grep java表示查看所有进程里 CMD 是 java 的进程信息2.ps -aux | grep ...
- my_interface
import flask,osserver=flask.Flask(__name__) #当前这个python文件,当做一个服务 @server.route('/error',methods=['ge ...
- 会话管理之Cookie技术
会话管理是web开发中比较重要的环节,这一节主要总结下会话管理中的cookie技术. 1. 何为会话 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个 ...
- 12个高效的VS调试技巧
介绍 调试是软件开发周期中的一个很重要的部分,有时很有挑战性,有时候则让程序员迷惑,有时候让程序员发疯,但是.可以肯定的是,对于任何不是太那个微不足道的程序来说,调试是不可避免的.近年来,调试工具的发 ...
- 数据库表设计时一对一关系存在的必要性 数据库一对一、一对多、多对多设计 面试逻辑题3.31 sql server 查询某个表被哪些存储过程调用 DataTable根据字段去重 .Net Core Cors中间件解析 分析MySQL中哪些情况下数据库索引会失效
数据库表设计时一对一关系存在的必要性 2017年07月24日 10:01:07 阅读数:694 在表设计过程中,我无意中觉得一对一关系觉得好没道理,直接放到一张表中不就可以了吗?真是说,网上信息什么都 ...