POJ1363Rails
来源:互联网 发布:剪切一半的数据丢失了 编辑:程序博客网 时间:2024/06/05 03:06
题目链接 :http://poj.org/problem?id=1363
此题是一个模拟栈的问题 题目意思:有一列火车,车厢编号是1~N,只能由一端进站,另一端出站,判断下列出站顺序是否有可能
思路:判断序列元素是否与栈顶元素相等,若与栈顶元素相等,则栈顶指针递减,继续判断
代码:注意输出Yes和输出格式
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
int main()
{
int n,a[1100];
while(scanf("%d",&n)!=EOF,n)
{
scanf("%d",&a[1]);
if(a[1]==0)
printf("\n");
while(a[1]!=0)
{
for(int i=2; i<=n; i++)
{
scanf("%d",&a[i]);
}
int j=1;
int top=0;
int stack[1100];
for(int i=1; i<=n; i++)
{
stack[++top]=i;
while(top&&stack[top]==a[j])
{
top--;
j++;
}
}
if(top==0)
printf("Yes\n");
else
printf("No\n");
scanf("%d",&a[1]);
if(a[1]==0)
printf("\n");
}
}
return 0;
}
此题是一个模拟栈的问题 题目意思:有一列火车,车厢编号是1~N,只能由一端进站,另一端出站,判断下列出站顺序是否有可能
思路:判断序列元素是否与栈顶元素相等,若与栈顶元素相等,则栈顶指针递减,继续判断
代码:注意输出Yes和输出格式
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
int main()
{
int n,a[1100];
while(scanf("%d",&n)!=EOF,n)
{
scanf("%d",&a[1]);
if(a[1]==0)
printf("\n");
while(a[1]!=0)
{
for(int i=2; i<=n; i++)
{
scanf("%d",&a[i]);
}
int j=1;
int top=0;
int stack[1100];
for(int i=1; i<=n; i++)
{
stack[++top]=i;
while(top&&stack[top]==a[j])
{
top--;
j++;
}
}
if(top==0)
printf("Yes\n");
else
printf("No\n");
scanf("%d",&a[1]);
if(a[1]==0)
printf("\n");
}
}
return 0;
}
阅读全文
0 0
- poj1363Rails
- poj1363Rails
- poj1363Rails
- POJ1363Rails
- poj1363Rails(栈模拟)
- poj1363Rails(栈)
- java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false is
- 【Spring】AOP
- 如何理解云计算?很简单,就像吃货想吃披萨了...
- JS闭包、自执行函数小记
- .NET Standard@Xamarin.Forms
- POJ1363Rails
- Node笔记
- 数据库事务及锁机制
- 仿淘宝鼠标悬浮一个区域,该区域显示一个层级块
- 编译安装mysql5.7
- 集合深浅拷贝以及经常遇到的坑(面试常问)
- ubuntu16.04 安装 opencv3.3.0 源代码编译
- PDO方式操作MYSQL的使用
- 进程间通信-消息队列