栈 练习 Codevs 3137 3138 3139

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

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int tack[1200],top,x,y,n;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        if(x==1){
            scanf("%d",&y);
            tack[++top]=y;
        }
        else top--;
    }
    if(top>=1) printf("%d",tack[top]);
    else printf("impossible!");

    return 0;
}

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

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 int tack[1200000],top,x,y,n;
 6 int main()
 7 {
 8     scanf("%d",&n);
 9     for(int i=1;i<=n;i++){
10         scanf("%d",&x);
11         if(x==1){
12             scanf("%d",&y);
13             tack[++top]=y;
14         }
15         else {
16             if(top>=1) top--;
17             else {
18                 printf("impossible!\n");return 0;
19             }
20         }
21     }
22     if(top>=1) printf("%d",tack[top]);
23     else printf("impossible!");
24
25     return 0;
26 }

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

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 int tack[1200000],top,x,y,n;
 6 int main()
 7 {
 8     scanf("%d",&n);
 9     for(int i=1;i<=n;i++){
10         scanf("%d",&x);
11         if(x==1){
12             scanf("%d",&y);
13             tack[++top]=y;
14         }
15         if(x==2) top--;
16         if(x==3) printf("%d\n",tack[top]);
17     }
18     return 0;
19 }
时间: 2024-08-18 11:51:00

栈 练习 Codevs 3137 3138 3139的相关文章

[自制简单操作系统] 3、内存管理和窗口叠加

1.本次主要进展 >_<" 这次主要学习了系统内存管理和窗口叠加~由于上两篇都做了详细的框架说明和介绍,这里直接上代码! 2.文件及函数构成 >_<" 这里和第二篇相比,把鼠标和键盘的相关函数独立出来放进各自相应的文件中,并主要在内存管理和窗口叠加进行探索,同时还有部分代码整理~ 1 /* In this file, not only have the defination of the function, but also 2 hava the descrip

[自制简单操作系统] 2、鼠标及键盘中断处理事件[PIC\GDT\IDT\FIFO]

1.大致介绍: >_<" 大致执行顺序是:ipl10.nas->asmhead.nas->bootpack.c PS: 这里bootpack.c要调用graphic.c.dsctbl.c.fifo.c.int.c实现功能,其中有些函数还必须汇编来写,所以单独写一个汇编文件naskfunc.nas,为了方便看全部函数和结构体,所以写一个bootpack.h来写一些结构体和函数声明~ >_<" 下面是编译图解:最终生成的haribote.img可放在软盘

UIViewController---汇总

1 UIWindow.UILabel.UIColor.UIScreen.UIViewController.UIView.UIControl.UIButton.IBOutlet.IBAction.UIStepper. UISlider. UISwitch.UITextField.UIAlertView.UIActionSheet.UINavigationController.UIBarButtonItem.UIImageView.UIScrollView.UIPageContro.UITableV

立即执行函数(IIFE)的理解与运用

作为JavaScript的常用语法,立即执行函数IIFE(Immediately-Invoked Function Expression)是值得我们认真去学习探究的. 一.创建函数的两种方式 我们先从基础讲起,要创建一个JS函数,有两种方式. (一)函数定义(Function Declaration) function Identifier ( Parameters ){ FunctionBody } 函数定义中,参数(Parameters)标识符(Identifier )是必不可少的.如果遗漏

System.Windows.Forms

1 File: winforms\Managed\System\WinForms\DataGridView.cs 2 Project: ndp\fx\src\System.Windows.Forms.csproj (System.Windows.Forms) 3 4 //------------------------------------------------------------------------------ 5 // <copyright file="DataGridVi

10000以内unicode对照表

0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8   9   10   11   12   13 . 14 . 15 . 16 . 17 . 18 . 19 . 20 . 21 . 22 . 23 . 24 . 25 . 26 . 27 . 28 . 29 . 30 . 31   32 ! 33 " 34 # 35 $ 36 % 37 & 38 ' 39 ( 40 ) 41 * 42 + 43 , 44 - 45 . 46 / 47 0 48 1 49 2 50 3 5

Hsql中In没有1000的限制

SELECT * FROM user where id in (1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 5

codevs 3139 栈练习3

3139 栈练习3 提交地址:http://codevs.cn/problem/3139/ 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 比起第一题,本题加了另外一个操作,访问栈顶元素(编号3,保证访问栈顶元素时或出栈时栈不为空),现在给出这N此操作,输出结果. 输入描述 Input Description N N次操作(1入栈 2出栈 3访问栈顶) 输出描述 Output Description K行(K为输入中询问的个数)

python之旅2

python基础 1整数 查看整数类型的方法 >>> a = 1 >>> dir(a) ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getn