Uva 514 Rails

注意輸出格式ios

 

 1 /*對於已經給出的入棧順序判斷出棧順序是否正確*/
 2 
 3 #include<iostream>
 4 #include<stack>
 5 #include<cstdio>
 6 using namespace std;
 7 const int num = 1000 + 10;
 8 int target[num];
 9 int main()
10 {
11     int n, i;
12     scanf_s("%d", &n);
13     while (n) {
14         cin >> target[1];
15         if (!target[1])//輸出格式格式
16         {
17             printf("\n");
18             cin >> n; 
19             continue;
20         }
21         for (i = 2; i <= n; i++)
22             cin >> target[i];
23         int A = 1, B = 1, ok = 1;
24         stack<int> s;
25         while (B <= n)
26         {
27             if (A == target[B]) { A++, B++; }//判斷有沒有直接入棧出棧的
28             else if (!s.empty() && s.top() == target[B]) { s.pop(); B++; }//判斷是否符合順序出棧規則
29             else if (A <= n) { s.push(A); A++; }//當A數所有入棧,此功能失效
30             else { ok = 0; break; }
31         }
32         printf("%s\n", ok ? "Yes" : "No");
33     }
34     return  0;
35 
36 }
相關文章
相關標籤/搜索