码迷,mamicode.com
首页 > 其他好文 > 详细

pta编程题5 Pop Sequence

时间:2018-03-30 21:54:06      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:iostream   else   oid   push   ret   --   div   结果   函数   

第一次提交结果都是YES,后来检查发现Push,Pop函数写的有问题,即Stack sta改为引用Stack &sta,否则不能改变实参的值。

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 
 5 int M;// 栈最大容量
 6 struct Stack
 7 {
 8    int arr[1000];
 9    int top = -1;
10 };
11 
12 void push(struct Stack &s, int i);
13 void pop(struct Stack &s);
14 bool isEmpty(struct Stack s);
15 int top(struct Stack s);
16 bool valid (vector<int> v);
17 
18 int main()
19 {
20     int n, k, i, j, t;
21     cin >> M >> n >> k;
22     int maxPop = 0;
23     vector<int> v(n);
24     for (i = 0; i < k; i++)
25     {
26         for (j = 0; j < n; j++)
27             cin >> v[j];
28         if (valid(v)) cout << "YES" << endl;
29         else cout << "NO" << endl;
30     }
31     return 0;
32 }
33 
34 void push(struct Stack &s, int i)
35 {
36     s.arr[++s.top] = i;
37 }
38 
39 void pop(struct Stack &s)
40 {
41     s.top--;
42 }
43 
44 bool isEmpty(struct Stack s)
45 {
46     return s.top == -1;
47 }
48 
49 int top(struct Stack s)
50 {
51     return s.arr[s.top];
52 }
53 
54 bool valid (vector<int> v)
55 {
56     struct Stack sta;
57     int i, j, maxPop = 0;
58     for (i = 0; i < v.size(); i++)
59     {
60         if (v[i] > maxPop)
61         {
62             for (j = maxPop + 1; j < v[i]; j++)
63                 push(sta, j);
64             if (sta.top + 2 > M) return false;
65             maxPop = v[i];
66         }
67         else if (!isEmpty(sta) && v[i] == top(sta))
68             pop(sta);
69         else if (!isEmpty(sta) && v[i] < top(sta))
70             return false;
71     }
72     return true;
73 }

 

pta编程题5 Pop Sequence

标签:iostream   else   oid   push   ret   --   div   结果   函数   

原文地址:https://www.cnblogs.com/lxc1910/p/8678209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!