UVA 514 Rails(栈)
2015-07-28 09:54
447 查看
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=455
题 意:给你一个组数,问将1到n一次入栈后,能否按此顺序输出。
思 路:定义一个栈来维护就行了。
代码如下:
题 意:给你一个组数,问将1到n一次入栈后,能否按此顺序输出。
思 路:定义一个栈来维护就行了。
代码如下:
#include <iostream> using namespace std; #include <string.h> #include <stdio.h> #include <stack> #include <algorithm> int vis[1200]; int main() { int n; while( scanf ( "%d", &n ) != EOF ) { stack<int>st; if( n == 0 ) break; while( 1 ) { for( int i = 1; i <= n; i ++ ) { scanf ( "%d", &vis[i] ); if(vis[1]==0) break; } if( vis[1] == 0 ) break; int a = 1, b = 1, ans = 1; while( b <= n ) { if( a == vis[b] ) { a++; b++; } else if( !st.empty() && st.top() == vis[b] ) { st.pop(); b++; } else if( a <= n ) st.push(a++); else { ans=0; break; } } if( ans == 1 ) printf("Yes\n"); else printf("No\n"); } printf("\n"); } return 0; }
相关文章推荐
- C++基于栈实现铁轨问题
- C语言栈的表示与实现实例详解
- C语言实现颠倒栈的方法
- 算法系列15天速成 第十天 栈
- 一看就懂:图解C#中的值类型、引用类型、栈、堆、ref、out
- Array栈方法和队列方法的特点说明
- Javascript SHA-1:Secure Hash Algorithm
- java数据结构之java实现栈
- 浅析栈区和堆区内存分配的区别
- 用PHP解决的一个栈的面试题
- 浅谈C#中堆和栈的区别(附上图解)
- JavaScript数据结构与算法之栈详解
- C语言之栈和堆(Stack && Heap)的优缺点及其使用区别
- Python实现栈的方法
- Java中堆和栈的区别详解
- java中堆和栈的区别分析
- C/C++函数调用栈的实现方法
- C++中静态存储区与栈以及堆的区别详解
- C++中用栈来判断括号字符串匹配问题的实现方法
- C++中栈结构建立与操作详细解析