poj 1363 Rails

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13192 Accepted: 5312

一道题很简单在很多的oj上都有这样的题目,利用栈的方法很容易解决,因为只有栈顶的元素才能出栈,这与本题的模型一样,所以采用栈的方法

代码:

 

  
    
1 #include < stdio.h >
2 #include < stack >
3   using namespace std;
4   int main()
5 {
6 int cur,i,j,a,n,mark;
7 while (scanf( " %d " , & n) != EOF)
8 {
9 if (n == 0 )
10 break ;
11 while ( 1 )
12 {
13 stack < int > st;
14 i = 1 ;st.push(i);
15 mark = 0 ;
16 for (j = 1 ;j <= n;j ++ )
17 {
18 scanf( " %d " , & a);
19 if (a == 0 )
20 {
21 mark = 1 ;
22 break ;
23 }
24 if (st.empty())
25 {
26 i ++ ;
27 st.push(i);
28 }
29 cur = st.top();
30 while (cur != a)
31 {
32 if (i == n)
33 break ;
34 i ++ ;
35 st.push(i);
36 cur = st.top();
37 }
38 if (cur == a)
39 st.pop();
40 }
41 if (mark)
42 break ;
43 if (st.empty())
44 printf( " Yes\n " );
45 else
46 printf( " No\n " );
47 }
48 printf( " \n " );
49 }
50 return 0 ;
51 }
52

 

你可能感兴趣的:(Rails)