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);
}
}
}
- 120 - Stacks of Flapjacks
- 120 - Stacks of Flapjacks
- 120 - Stacks of Flapjacks
- 120 - Stacks of Flapjacks
- 120 - Stacks of Flapjacks
- 120 - Stacks of Flapjacks
- 120 - Stacks of Flapjacks
- 120 - Stacks of Flapjacks
- 120 - Stacks of Flapjacks
- UVA 120 Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks
- uva 120 Stacks of Flapjacks
- UVA 120 - Stacks of Flapjacks
- uva 120 - Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks
- UVaOJ 120 - Stacks of Flapjacks
- UVA 120 - Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks
- uvalive 4850(贪心)
- 陀螺仪
- Android Studio功能
- Android开发之性能调优工具
- leetcode:Valid Palindrome 菜鸟解法
- 120 - Stacks of Flapjacks
- hibernate:java.math.BigInteger cannot be cast to java.lang.Long
- 714 - Copying Books
- 1152 - 4 Values whose Sum is 0
- 1442 - Cav
- 1451 - Average
- 网站加速--服务器编写篇(转)
- 计算器的实现——基础篇(一)
- jetty嵌入式 webApp开发(Simple Demo)