codevs 3137-3139 栈练习 x

3中 换行需谨慎!!!一定要注意换行!!!

3137 栈练习1

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
题目描述 Description

给定一个栈(初始为空,元素类型为整数,且小于等于100),只有两个操作:入栈和出栈。先给出这些操作,请输出最终栈的栈顶元素。  操作解释:1表示入栈,2表示出栈

输入描述 Input Description

N(操作个数)

N个操作(如果是入栈则后面还会有一个入栈元素)

具体见样例(输入保证栈空时不会出栈)

输出描述 Output Description

最终栈顶元素,若最终栈空,输出”impossible!”(不含引号)

样例输入 Sample Input

3

1 2

1 9

2

样例输出 Sample Output

2

数据范围及提示 Data Size & Hint

对于100%的数据  N≤1000 元素均为正整数且小于等于100

分类标签 Tags 点此展开 

 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 struct Q{//手写栈
 8     int top;
 9     int stack[1000001];
10     Q ()
11     {
12         top=0;
13     }
14     void pop()
15     {
16         top--;
17     }
18     int push(int x)
19     {
20         stack[++top]=x;
21     }
22     int tops()
23     {
24         return stack[top];
25     }
26 }q;
27 
28 int main()
29 {
30     int n,top=0,x,a,b;
31     scanf("%d",&n);
32     while(n--)
33     {
34         cin>>x;
35         switch(x)
36         {
37             case 1:
38                 cin>>a;
39                 q.push(a);
40                 break;
41             case 2:
42                 if(q.top>0) 
43                 q.pop();
44                 else {cout<<"impossible!"; return 0;}
45                 break;
46         }
47     }
48     if(q.top==0) cout<<"impossible!";
49     else
50         cout<<q.tops();
51     return 0;
52 }

3138 栈练习2

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
 
题目描述 Description

(此题与栈练习1相比改了2处:1加强了数据 2不保证栈空时不会出栈)

给定一个栈(初始为空,元素类型为整数,且小于等于100),只有两个操作:入栈和出栈。先给出这些操作,请输出最终栈的栈顶元素。  操作解释:1表示入栈,2表示出栈

输入描述 Input Description

N(操作个数)

N个操作(如果是入栈则后面还会有一个入栈元素)

具体见样例(输入不保证栈空时不会出栈)

输出描述 Output Description

最终栈顶元素,若最终栈空,或栈空时有出栈操作,输出”impossible!”(不含引号)

样例输入 Sample Input

3

1 2

2

2

样例输出 Sample Output

impossible!

数据范围及提示 Data Size & Hint

对于100%的数据  N≤100000 元素均为正整数且小于等于10^8

分类标签 Tags 点此展开 

 
 
加强版???数组大一点就行
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 struct Q{
 8     int top;
 9     int stack[1000001];
10     Q ()
11     {
12         top=0;
13     }
14     void pop()
15     {
16         top--;
17     }
18     int push(int x)
19     {
20         stack[++top]=x;
21     }
22     int tops()
23     {
24         return stack[top];
25     }
26 }q;
27 
28 int main()
29 {
30     int n,top=0,x,a,b;
31     scanf("%d",&n);
32     while(n--)
33     {
34         cin>>x;
35         switch(x)
36         {
37             case 1:
38                 cin>>a;
39                 q.push(a);
40                 break;
41             case 2:
42                 if(q.top>0) 
43                 q.pop();
44                 else
45                 {
46                     cout<<"impossible!";
47                     return 0;
48                 }
49                 break;
50         }
51     }
52     if(q.top==0) cout<<"impossible!";
53     else
54         cout<<q.tops();
55     return 0;
56 }

3139 栈练习3

 

 时间限制: 2 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
 
题目描述 Description

比起第一题,本题加了另外一个操作,访问栈顶元素(编号3,保证访问栈顶元素时或出栈时栈不为空),现在给出这N此操作,输出结果。

输入描述 Input Description

N

N次操作(1入栈 2出栈 3访问栈顶)

输出描述 Output Description

K行(K为输入中询问的个数)每次的结果

样例输入 Sample Input

6

1  7

3

2

1  9

1  7

3

样例输出 Sample Output

7

7

数据范围及提示 Data Size & Hint

 对于50%的数据 N≤1000 入栈元素≤200

 对于100%的数据 N≤100000入栈元素均为正整数且小于等于10^4 

分类标签 Tags 点此展开 

 
 
 
添加了第三个操作。
代码:
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 struct Q{
 8     int top;
 9     int stack[1000001];
10     Q ()
11     {
12         top=0;
13     }
14     void pop()
15     {
16         top--;
17     }
18     int push(int x)
19     {
20         stack[++top]=x;
21     }
22     int tops()
23     {
24         return stack[top];
25     }
26 }q;
27 
28 int main()
29 {
30     int n,top=0,x,a,b;
31     scanf("%d",&n);
32     while(n--)
33     {
34         cin>>x;
35         switch(x)
36         {
37             case 1:
38                 cin>>a;
39                 q.push(a);
40                 break;
41             case 2:
42                 q.pop();
43                 break;
44             case 3:
45                 cout<<q.tops()<<endl;//就是这里!一定要注意换行 
46                 break;
47         }
48     }
49     return 0;
50 }

 

posted @ 2017-04-27 10:27  夜雨声不烦  阅读(166)  评论(0编辑  收藏  举报