• codevs 3185-3187 队列练习x


    三联水题……
     
    3185x
                        
    题目描述 Description

    给定一个队列(初始为空),只有两种操作入队和出队,现给出这些操作请输出最终的队头元素。 操作解释:1表示入队,2表示出队

    输入描述 Input Description

    N(操作个数)
    N个操作(如果是入队则后面还会有一个入队元素)
    具体见样例(输入保证队空时不会出队)

    输出描述 Output Description

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

    样例输入 Sample Input

    3
    1 2
    1 9
    2

    样例输出 Sample Output

    9

    数据范围及提示 Data Size & Hint

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

    分类标签 Tags

    #include<cstdio>
    #include<iostream>
    
    using namespace std;
    
    int n,top=0,s=0,h=0;
    int a[111111];
    
    void pop()
    {
        int v;
        scanf("%d",&v);
        a[h++]=v;
    }
    
    void push()
    {
        top++;
    }
    
    int main()
    {
        int p;
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&p);
            if(p==1)
            {
                s++;
                pop();
            }
            if(p==2)
            {
                s--;
                push();
            }
        }
        if(s<=0)
        {
            printf("impossible!");
        }
        else printf("%d",a[top]);
        return 0;
    }
    3186x
                        
    题目描述 Description

    (此题与队列练习1相比改了2处:1加强了数据 2不保证队空时不会出队)
    给定一个队列(初始为空),只有两种操作入队和出队,现给出这些操作请
    输出最终的队头元素。 操作解释: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

    #include<cstdio>
    #include<iostream>
    
    using namespace std;
    
    int n,top=0,s=0,h=0;
    int a[111111];
    bool b=0;
    
    void pop()
    {
        int v;
        scanf("%d",&v);
        a[h++]=v;
    }
    
    void push()
    {
        if(top<h)
        top++;
        else b=1;
    }
    
    int main()
    {
        int p;
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&p);
            if(p==1)
            {
                s++;
                pop();
            }
            if(p==2)
            {
                s--;
                push();
                if(b)
                {
                    printf("impossible!");
                    return 0;
                }
            }
            
        }
        if(s<=0)
        {
            printf("impossible!");
        }
        else printf("%d",a[top]);
        return 0;
    }

     

     

    3187x

                         题目描述 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
    9

    数据范围及提示 Data Size & Hint

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

    分类标签 Tags

    #include<cstdio>
    #include<iostream>
    
    using namespace std;
    
    int n,top=0,h=0;
    int a[111111];
    
    void pop()
    {
        int v;
        scanf("%d",&v);
        a[h]=v;h++;
        if(h==1)
        {
            a[top]=v;
        }
    }
    
    void push()
    {
        top++;
    }
    
    int main()
    {
        int p;
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&p);
            if(p==1)
            {
                pop();
            }
            if(p==2)
            {
                push();
            }
            if(p==3)
            {
                printf("%d
    ",a[top]);
            }
        }
        return 0;
    }

    如果运气好也是错,那我倒愿意错上加错!

    ❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀

  • 相关阅读:
    [转]技术Leader思考方法
    [转]谈谈技术能力
    程序员最浪漫的表白方式,将情书写在她的照片里,Python简直太厉害啦~
    Python绘制饼状图对商品库存进行分析
    不怕新歌有多嗨,就怕老歌带DJ,用Python批量下载dj歌曲!
    趁这个软件还没倒闭,我连夜用Python下载了所有壁纸...
    如何让电脑永不息屏?Python:这事我熟,只需5行代码...
    Python告别pip手动安装模块,实现全自动安装第三方库,彻底解放你的双手
    网易云VIP歌曲没权限?还好我会Python,一分钟一个歌单,硬盘有点不够用了~
    Python采集疫情数据,绘制可视化动态地图,实时查询疫情数据!
  • 原文地址:https://www.cnblogs.com/zxqxwnngztxx/p/6665509.html
Copyright © 2020-2023  润新知