Uva 514 Rails(栈应用)
2016-05-02 16:23
591 查看
Uva 514 Rails
原题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=455
题意: 1-n编号的车从A驶入车站然后出站到B 问能否按照你输入的顺序输出 (进入中转站Station可以暂时停在里面 要满足后进先出)简单的栈应用
#include <stdio.h>
int main()
{
const int MAXN=10010;
int t[MAXN],n;
while(scanf("%d",&n)&&n!=0)
{
while(scanf("%d",&t[1]))
{
int stack[MAXN];
int p=0,i,a=1,b=1;
int c=1;
if(t[1]==0)break;
for(i=2; i<=n; i++)
{
scanf("%d",&t[i]);
}
while(b<=n)
{
if(a==t[b])
{
a++;
b++;
}
else if(p && stack[p]==t[b])
{
p--;
b++;
}
else if(a<=n) stack[++p]=a++;
else
{
c=0;
break;
}
}
printf("%s\n",c ? "Yes" : "No");
}
printf("\n");
}
return 0;
}
原题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=455
题意: 1-n编号的车从A驶入车站然后出站到B 问能否按照你输入的顺序输出 (进入中转站Station可以暂时停在里面 要满足后进先出)简单的栈应用
#include <stdio.h>
int main()
{
const int MAXN=10010;
int t[MAXN],n;
while(scanf("%d",&n)&&n!=0)
{
while(scanf("%d",&t[1]))
{
int stack[MAXN];
int p=0,i,a=1,b=1;
int c=1;
if(t[1]==0)break;
for(i=2; i<=n; i++)
{
scanf("%d",&t[i]);
}
while(b<=n)
{
if(a==t[b])
{
a++;
b++;
}
else if(p && stack[p]==t[b])
{
p--;
b++;
}
else if(a<=n) stack[++p]=a++;
else
{
c=0;
break;
}
}
printf("%s\n",c ? "Yes" : "No");
}
printf("\n");
}
return 0;
}
相关文章推荐
- HDU 1789 Doing Homework again
- what's the cygwin and how cai i install this software
- 70. Climbing Stairs
- 服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
- 70. Climbing Stairs
- container_of 宏、offsetof 宏 分析
- Flex AIR ANT 脚本编译
- CodeForces 667A Pouring Rain
- CodeForces 667A Pouring Rain
- hdu2925 Musical Chairs && poj3517 And Then There Was One(约瑟夫环)
- Raid 级别
- Hibernate运行报错Unknown entity: domain.UserBean
- HDU 3394 Railway 点双连通分量 + 桥
- Rails,ACM/ICPC CERC 1997,UVa514
- Insufficient memory<failed to allocate 232852> in cv::OutOfMemoryError
- LeetCode 217. Contains Duplicate
- codeforces 667A A. Pouring Rain(水题)
- poj2010 Moo University - Financial Aid(最大化第k大)
- leetcode 070 climbing stairs
- Coremail手机版页面持久型XSS实践