题目链接:http://poj.org/problem?id=1363

题意:有一列火车,车厢编号为1~n,从A方向进站,向B方向出站。现在进站顺序确定,给出一个出站的顺序,判断出站顺序是否合理。

实际上是模拟栈的过程,而栈的特点是先进后出。另外一个麻烦的地方就是输入输出格式问题。

本题实现提供两种方法:没有用到STL栈和有用到STL栈

 #include <iostream>      // 法二:头文件多包含一个 #include <stack>
using namespace std; const int maxn = + ; int main()
{
int A, B, i, n, top, target[maxn], stack[maxn]; // 法二:数组stack去掉, 变成 stack<int> s
while (scanf("%d", &n) && n)
{
while ()
{
int flag = ;
for (i = ; i <= n; i++)
{
scanf("%d", &target[i]);
if (target[] == )
{
flag = ;
break;
}
}
if (!flag)
{
printf("\n");
break;
}
int ok = ;
A = B = ;
top = ;
while (B <= n)
{
if (A == target[B]) // 处理1~n的特殊情况
{
A++, B++;
}
else if (top && stack[top] == target[B]) // top && !s.empty() && s.top() == target[B] 出栈
{
top--; // s.pop();
B++;
}
else if (A <= n)
{
stack[++top] = A; // s.push(A); 进栈
A++;
}
else
{
ok = ;
break;
}
}
if (ok)
printf("Yes\n");
else
printf("No\n");
}
}
return ;
}

poj 1363 Rails 解题报告的更多相关文章

  1. POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)

    经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...

  2. POJ 1363 Rails(栈)

    题目代号:POJ 1363 题目链接:http://poj.org/problem?id=1363 题目原题: Rails Time Limit: 1000MS   Memory Limit: 100 ...

  3. OpenJudg / Poj 1363 Rails

    1.链接: http://poj.org/problem?id=1363 http://bailian.openjudge.cn/practice/1363 2.题目: Rails Time Limi ...

  4. 【原创】poj ----- 1182 食物链 解题报告

    题目地址: http://poj.org/problem?id=1182 题目内容: 食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  5. poj 2051.Argus 解题报告

    题目链接:http://poj.org/problem?id=2051 题目意思:题目有点难理解,所以结合这幅图来说吧---- 有一个叫Argus的系统,该系统支持一个 Register 命令,输入就 ...

  6. poj 1102.LC-Display 解题报告

    题目链接:http://poj.org/problem?id=1102 题目意思:就是根据给出的格式 s 和 数字 n,输出数值 n 的 LCD 显示.数值 n 的每个数字要占据 s + 2 列 和 ...

  7. POJ 1363 Rails

    Rails Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21728   Accepted: 8703 Descriptio ...

  8. POJ 1840 Eps 解题报告(哈希)

    a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0,xi∈[-50,50],且xi!=0.让我们求所有解的可能. 首先,如果暴力判断的话,每个x的取值有100种可能,100^5肯定 ...

  9. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

随机推荐

  1. 【HDU 1757】 A Simple Math Problem

    题 Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 1 ...

  2. 【poj1745】 Divisibility

    http://poj.org/problem?id=1745 (题目链接) 题意 给出n串数,可以在其两两之间添加+或-,判断是否存在某种方案使得出的表达式的答案可以整除k. Solution 水题一 ...

  3. 【codevs1257】 打砖块

    http://codevs.cn/problem/1257/ (题目链接) 题意 在等腰三角形上打砖块,总共有m发炮弹,每块砖有一个权值,求打出的最大权值 Solution 今天考试题,考场上的2个小 ...

  4. codeforces 58E:Expression

    Description One day Vasya was solving arithmetical problems. He wrote down an expression a + b = c i ...

  5. CodeForces 559C Gerald and Giant Chess

    C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. Erlang第三课 ---- 创建和使用module

    ----------------小技巧----------------------------- 因为这一课开始,我们要使用Erlang文件操作,所以,我们期待启动shell的时候,当前目录最好是是我 ...

  7. C#通过编程方式实现Ping

    代码是照着书敲的,贴出来方便平时参考 using System; using System.Collections.Generic; using System.Linq; using System.T ...

  8. 使用Jquery+EasyUI 进行框架项目开发案例讲解之五 模块(菜单)管理源码分享

    http://www.cnblogs.com/huyong/p/3454012.html 使用Jquery+EasyUI 进行框架项目开发案例讲解之五  模块(菜单)管理源码分享    在上四篇文章 ...

  9. Android实现控件动画效果

    MainActivity.java public class MainActivity extends AppCompatActivity { private ImageView iv; privat ...

  10. Ubuntu格式化分区时的一个小错误

    admin@gpc:/$ sudo  mkfs -t ext3 -c /dev/sdb1 mkfs.ext3: inode_size (128) * inodes_count (0) too big ...