zoukankan      html  css  js  c++  java
  • BUPT复试专题—List(2015)

    题目描述

    在该LIST上实现3种操作

             1、append x在该LIST末尾添加x,x是32位整数

             2、pop删除该LIST末尾的数

             3、find i寻找第i个数,若i为负数表示寻找倒数第i个数,例如i = -1表示寻找倒数第一个

    输入

    首先一个数t表示以下有t个m

    第一行输入一个m,表示有m条操作,接下来每行输入一条操作

     

    输出

    当输入find i时输出找到的数

    样例输入

    2 
    5 
    append 1 
    append 2 
    find 1 
    find -1 
    pop 
    6 
    append 1 
    append 2 
    append 3 
    append 4 
    find -2 
    find 2
    

    样例输出

    1
    2
    3
    2

    来源

    2015机考B题 

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    using namespace std;
    int main()
    {
        int num=0;
        scanf("%d",&num);
        while(num--)
        {
            int m,strnum=0;
            scanf("%d",&m);
            while(m--)
            {
                string temp,num1,num;
                char str[100];
                cin>>temp;
                if(temp[0]!='p')
                    cin>>num1;
                if(temp[0]=='a')//append
                {
                    str[strnum]=num1[0];
                    strnum++;
                }
                else if(temp[0]=='f')//find
                {
                    int x=num1[0]-48;
                    if(x<0)
                    {
                        x=num1[1]-48;
                        x=strnum-x+1;
                    }
                    cout<<str[x-1]<<endl;
                }
                else if(temp[0]=='p')//pop
                {
                    strnum--;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    398. Random Pick Index
    382. Linked List Random Node
    645. Set Mismatch
    174. Dungeon Game
    264. Ugly Number II
    115. Distinct Subsequences
    372. Super Pow
    LeetCode 242 有效的字母异位词
    LeetCode 78 子集
    LeetCode 404 左叶子之和
  • 原文地址:https://www.cnblogs.com/dzzy/p/8485165.html
Copyright © 2011-2022 走看看