下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、题目三、RailsDescriptionThere is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be
2、 only a dead-end one (see picture) and due to lack of available space it could have only one track.The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N = 1000
3、 coaches numbered in increasing order 1, 2, ., N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, ., aN. Help him and write a program that decides whether it is possible to get the required orde
4、r of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station.
5、But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.InputThe input consists of blocks of lines. Each block except the last describes one train and possibly more require
6、ments for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, ., N. The last line of the block contains just 0. The last block consists of just one line containing 0. OutputThe output contai
7、ns the lines corresponding to the lines with permutations in the input. A line of the output contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains No. In addition, there is one empty line after the lines correspondin
8、g to one block of the input. There is no line in the output corresponding to the last null block of the input.Sample Input51 2 3 4 55 4 1 2 366 5 4 3 2 1Sample OutputYesNoYes程序清单:(STL标准模板库)一、#include#include#includeusing namespace std;int a1001,n,i,j;int fan()stacks; /火车是按(1-n)的顺序进栈inti,j;for(i=0,j=
9、1;in&j=n+1;)if(s.empty()|s.top()!=ai)/栈空或栈顶元素不与ai相同时进站 if(j=(n+1) /不能正常出站条件 return 0; /不能正常出站返回0 s.push(j+); /将j压入栈中else /栈顶元素与ai相同时进站s.pop(); /弹出栈顶元素i+; /i+,a的下个元素继续进行return 1;/*fan()解释:1-n和an的每个元素进行比较。用j=1,j+表示1n的每个元素用ai(0=in)表示ai的每个元素。如果栈为空时让j进栈,然后j+。如果栈顶元素与ai不相同,让j进栈,然后j+。如果栈顶元素与ai相同,弹出栈顶元素,然后i+
10、。结束条件:1、j=(n+1)时表示不能按给出的顺序出站,则返回02、循环结束返回1*/int main()while(scanf(%d,&n),n)/有多少辆火车,以0结束。 while(scanf(%d,&a0),a0) /输入出站次序,以0结束 for(i=1;in;i+)scanf(%d,&ai); /把出站次序放在数组a中。if(fan()printf(Yesn); /可以出站输出Yeselseprintf(Non); /不可以正常出站输出Noreturn 0;运行结果:二、#include#include#includeusing namespace std;int a1001,n
11、,i,j,k;int fan()stacks;inti,j=0,k;for(i=1;i=n;i+)if(aj=i) /当aj与i相同时j+; /进行下一个a中的元素while(!s.empty()/栈不空时k=s.top(); /得到栈顶元素s.pop(); /弹出栈顶元素if(k=aj) /栈顶元素与aj相同,继续进行下一个 j+;else /栈顶元素与aj不同时s.push(k); /把k再压入栈中break; /if语句结束。else /当aj与i不相同时把i压入栈中s.push(i);if(s.empty() /栈空返回1return 1;else /栈不空返回0return 0;int main()while(scanf(%d,&n),n)while(scanf(%d,&a0),a0)for(i=1;in;i+)scanf(%d,&ai);if(fan()printf(Yesn);elseprintf(No
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年大气污染防治法城管职责题库
- 2026年青年干部黄河保护法专项竞赛题库
- 2026年历史常识与文化素养AI出题
- 2026年智能化工具应用管理晋升知识考核
- 2026年机器人研学旅行竞赛体验课程
- 2026年绿色环保技术突破题集
- 2026年人工智能赋能新质生产力题库
- 2026年题型多样化训练助你轻松备考过关
- 2026年女工特殊劳动保护规定与权益保障测试
- 2026年街道农贸市场及周边环境治理题
- 电离辐射危害及预防方法
- 系统解剖学课件:内脏神经
- GB/T 19515-2023道路车辆可再利用率和可回收利用率要求及计算方法
- GB/T 15587-2023能源管理体系分阶段实施指南
- ICD-9-CM3编码与手术分级目录
- 数据库原理及应用-课件
- 探究物联网的技术特征-说课
- GB/T 18804-2022运输工具类型代码
- LY/T 1726-2008自然保护区有效管理评价技术规范
- GA/T 951-2011紫外观察照相系统数码拍照规则
- 《内部控制》第四章-风险评估课件
评论
0/150
提交评论