下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 危废仓库防雷防静电方案
- PETB全国英语等级考试二级模拟试题(含详细答案解析)
- 2026注册安全工程师《建筑施工安全》考试真题及答案解析
- 地下管线巡检工安全操作规程
- 功能性消化不良合并失眠综合干预共识
- 2025-2026学年河北省沙河市初中政治七年级期末上册自测高频题(附答案)
- (2026)新版保安员考试试题附含答案
- OCP 19c 数据库认证考试试题及详细答案解析
- 电力防汛排涝应急处置方案
- 带状疱疹后神经痛全程管理专家共识(2025版)
- 肠外营养护理课件
- ASTM A681合金工具钢规范详细解析
- 2025~2026学年甘肃省兰州市第十九中学七年级上学期期中数学试卷
- 浙江省9+1高中联盟2025-2026学年高一上学期11月期中考试物理试卷(含答案)
- 皮肤沙龙活动方案
- 防火门更换施工方案
- 货物买卖合同模板(CIF条款)4篇
- 学堂在线 海权与制海权 结业考试答案
- 2024年房地产经纪协理考试题库(考试直接用)
- 水利建设管理
- 冲上云霄-飞机鉴赏知到智慧树章节测试课后答案2024年秋上海工程技术大学
评论
0/150
提交评论