poj1363(栈的入门)(一)

本文详细介绍了如何通过输入输出的数据结构理解铁路教练车重组问题,并提供了一个具体的C语言代码实现来解决该问题。通过分析输入数据块中的列车描述及其重组需求,算法能够判断是否有可能按照指定顺序重新排列火车车厢。本文不仅提供了问题的理论背景,还通过实例展示了实际的编程解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Rails

Input

The input consists of blocks of lines. Each block except the last describes one train and possibly more requirements 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.

Output

The output contains 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 corresponding to one block of the input. There is no line in the output corresponding to the last ``null'' block of the input.

Sample Input

5 1 2 3 4 5 5 4 1 2 3 0 6 6 5 4 3 2 1 0 0

Sample Output

Yes No Yes
 
 
# include<stdio.h> 


int main() 
{ 
int a[1024],b[1024],c[3000],i,j,n; 
int top;
 while(scanf("%d",&n),n!=0) 
{ 
while(scanf("%d",&b[0]),b[0]!=0) 
{      
a[0]=1;
         for(i=1;i<n;i++)
{
          a[i]=i+1;
          scanf("%d",&b[i]);
        } 
i=top=j=0;
 while(i<n) 
{  
c[top]=a[i];
 while(c[top]==b[j]&&j<n) 
{ 
top--; 
j++;
           } 
i++; 
top++; 
} 
if(j==n) 
printf("Yes\n"); 
else 
printf("No\n"); 
} 
printf("\n"); 
} 
return 0;
 } 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值