UVO 120 Stacks of Flapjacks (整行输入+构造函数)
2015-08-26 15:58
281 查看
Description
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.
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):
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).
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.
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.
水水题。给出煎饼数列, 一次只能让第一个到第i个数列全部反转,要求把数列排序为升序。
算法点破后不值几钱。。。
只要想办法把最大的煎饼放到最后一个,然后就变成前面那些煎饼的数列的子题目了。递归或循环即可。
把大饼放后面只要先让前面翻转使大饼排在后面,再整体翻转让大饼排到后面。
这题不是求最优解,我也不知道这样是不是最优解。
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 theoryof 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 isgiven 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 ison 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
水水题。给出煎饼数列, 一次只能让第一个到第i个数列全部反转,要求把数列排序为升序。
算法点破后不值几钱。。。
只要想办法把最大的煎饼放到最后一个,然后就变成前面那些煎饼的数列的子题目了。递归或循环即可。
把大饼放后面只要先让前面翻转使大饼排在后面,再整体翻转让大饼排到后面。
这题不是求最优解,我也不知道这样是不是最优解。
#include<iostream> #include<cstdio> #include<sstream> #include<cstring> #include<algorithm> using namespace std; int a[40],b[40],ans[40]; void sswap(int x) { int i,j; for(i=1,j=x;i<=x/2;i++,j--) { int temp=a[i]; a[i]=a[j]; a[j]=temp; } } int main() { int num,i,j,n; stringstream stream; string buf; while(getline(cin,buf)) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); i=0; int tem; stream<<buf; while(stream>>tem) { a[++i]=tem; b[i]=tem; } stream.clear(); for(int _=1;_<=i;_++) { if(_==1) printf("%d",a[_]); else printf(" %d",a[_]); } printf("\n"); n=i; int k=0; sort(b+1,b+i+1); for(i=n;i>=1;i--) { if(a[i]!=b[i]) { for(j=1;j<=i;j++) { if(a[j]==b[i]) break; } if(j!=1) { sswap(j); ans[k++]=n-j+1; } sswap(i); ans[k++]=n-i+1; } } ans[k++]=0; for(i=0;i<k;i++) { if(!i) printf("%d",ans[i]); else printf(" %d",ans[i]); } printf("\n"); } return 0; }
相关文章推荐
- 浅谈流形学习
- 并发编程网
- [LeedCode OJ]#162 Find Peak Element
- android使用library
- 安卓5.0新特性
- 倒计时
- 统计文件中单词个数
- webservice编程中的RPCServiceClient类在哪个jar包中?
- iOS开发--TDD的iOS开发初步以及Kiwi使用入门
- TextView和EditText的显示
- SPOJ COT Count on a tree(树上路径第k小 主席树)
- Mr. Process的一生-Linux内核的社会视角 (1) 调度
- Android Studio奇技淫巧一览 大幅度提升工作效率
- Office 365 IT Professional —— OneDrive
- Rust初步(六):在C#中使用Rust组件
- URL锚点定位
- IOS自带二维码生成
- 【转】Angularjs Controller 间通信机制
- linux查看机器负载
- 在SourceInsight中用快捷键打开文件所在的目录