




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 产品货物装运合同范例
- 学生如何安全使用社交媒体
- 学校操场安全监管系统建设方案
- 中国R1234yf制冷剂行业市场竞争现状及发展前景研判报告
- 2025四川长虹电子科技有限公司招聘主管电路设计工程师等岗位3人笔试参考题库附带答案详解
- 2025四川经准特种设备检验有限公司招聘50人笔试参考题库附带答案详解
- 2025哈密伊州机场第一季度招聘(18人)笔试参考题库附带答案详解
- 2025中国电力科学研究院有限公司高校毕业生招聘(第二批)笔试参考题库附带答案详解
- 中国人工智能产业的国际竞争力研究
- 2024-2025版新教材高中历史第九单元中华人民共和国成立和社会主义革命与建设第27课社会主义建设在探索中曲折发展练习含解析新人教版必修中外历史纲要上
- 自我管理能力试题及答案
- 邯郸2025年河北邯郸市春季博硕人才引进1438人笔试历年参考题库附带答案详解
- 中国特色社会主义政治经济学课件
- 2025年《中央一号文件》参考试题库资料100题及答案(含单选、多选、判断题)
- 2025年安徽林业职业技术学院单招职业技能测试题库及答案(考点梳理)
- 初中语文大单元整体教学设计研究
- 《烟草商业企业 客户服务质量评价指南》技术报告
- 2024-2025中考英语八大时态混合真题
- 临床医学个人能力提升
- 2025年焦虑症健康教育课件:创新与实践相结合
- 定额〔2025〕2号文-关于发布2020版电网技术改造及检修工程概预算定额2024年下半年价格
评论
0/150
提交评论