• BUPT复试专题—Python List(2014)


    题目描述

    在Python中,List (列表)是一种非常重要的数据结构。它与C/C++/Java中的 数组有些类似,但支持添加新元素时的动态扩展。在这个问题中,你需要处理如下 的几种对List的操作。
    • L=[]:将名字为L的List淸空。在这里,List的名字是长度为1到10之间的字符串(只包括大小写字母)。如果L原来不存在,这个语句相当于定义了一个名字为L的空列表。
    • L.append(x):向L的末端插入元素X。为方便起见,这里的x只会是 [0,65536]之间的整数。
    • L. sort ():将L中的元素按升序排序。
    • L[id]:返回L中下标为id(>=0)的值。下标是从0开始计数的。
    给定若干Python语句,你的任务是对于每个形如L[id]的语句,输出它返回的值。
     

    输入

    输入数据包含多组测试数据。请注意各组测试数据之间是相互独立的。
    输入的第一行是一个整数T(T<=100),表示测试数据的组数。
    每组测试数据第一行是语句的数量N(<=100)。接下来N行,每行一个python 语句。测试数据保证只会出现上述四种语句,语句中间不会出现空格。一个List在被使用前一定会被先定义。
     

    输出

    对于每个查询,输出査找的L[id]的值。如果id超出了当前List的下标范围, 输出一行ERROR。

    样例输入

    2
    5
    a=[]
    a.append(0)
    a.append(1)
    a[0]
    a[1]
    8
    lista=[]
    lista.append(123)
    lista.append(65)
    lista[0]
    lista.sort()
    lista[0]
    listb=[]
    listb[0]

    样例输出

    0
    1
    123
    65
    ERROR

    来源

    2014机考D题 

    这个题的陷阱在于每次操作的数组不一定只有一个

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <algorithm>
    using namespace std;
    struct pylist
    {
        int donser[105];
        string temp;
        int now;
    };
    int sor(const void *a,const void *b)
    {
        return *(int *)a-*(int *)b; //小到大 
    }
    int main()
    {
        int T,n;
        cin>>T;
        while(T--)
        {
            cin>>n;
            //int donser[105];
            pylist real_list[105];
            for(int i=0;i<20;i++)
            {
                real_list[i].temp="";
                real_list[i].now=-1;
                for(int j=0;j<100;j++)
                    real_list[i].donser[j]=0;
            }
            //int now=-1;
            while(n--)
            {
                string temp;
                cin>>temp;
                int pos1=temp.find('.'),pos2=temp.find('['),pos3=temp.find('='),pos=0;
                int shit=pos1;
                if(shit<0)    shit=pos2;
                if(pos3>0)  shit=pos3;
                string real_str=temp.substr(0,shit);
                //cout<<"*"<<real_str<<endl;
                if(real_list[0].temp!="")
                {
                    int i=0,temp_lable=0;
                    while(real_list[i].temp!=""&&!temp_lable)
                    {
                        //cout<<"+"<<real_list[i].temp<<" "<<real_str<<endl; 
                        if(real_list[i].temp==real_str) 
                        {
                            pos=i;
                            temp_lable=1;
                        }
                        i++;    
                    }
                    if(temp_lable==0)
                    {
                        pos=i;
                        real_list[i].temp=real_str;
                    }    
                }
                //cout<<"*"<<pos<<endl;
                if(real_list[0].temp=="")
                    real_list[0].temp=real_str;
                if(pos2!=-1)////////lista[num] or listb=[]
                {
                    if(temp[pos2+1]==']')
                    {
                        real_list[pos].now=-1;
                    }
                    else
                    {
                        int i=pos2+1;int num=temp[i]-'0';i++;
                        while(1)
                        {
                            if(temp[i]>='0'&&temp[i]<='9'&&i<temp.size())
                            {
                                num=num*10+(temp[i]-'0');
                                i++;
                            }
                            else
                                break;
                        }
                        if(real_list[pos].now<num)
                            cout<<"ERROR"<<endl;
                        else
                            cout<<real_list[pos].donser[num]<<endl;
                    }
                }
                if(pos1!=-1&&temp[pos1+5]!='(')///////////append()
                {
                    int i=pos1+8;int num=temp[i]-'0';i++;
                    while(1)
                        {
                            if(temp[i]>='0'&&temp[i]<='9'&&i<temp.size())
                            {
                                num=num*10+(temp[i]-'0');
                                i++;
                            }
                            else
                                break;
                        }
                    real_list[pos].now++;
                    real_list[pos].donser[real_list[pos].now]=num;
                }
                if(pos1!=-1&&temp[pos1+5]=='('&&real_list[pos].now!=-1)////sort()
                {
                    qsort(real_list[pos].donser,real_list[pos].now+1,sizeof(int),sor);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    Hive初识(一)
    图解HTTP总结(8)——确认访问用户身份的认证
    Android 7.0 照相 FileUriExposedException
    activity跳转的一些坑
    gopath配置
    android项目中记录
    一些趣味性总结(JAVA)
    http的response遇到illegalstateexception解决办法
    django demo
    Error:Execution failed for task ':app:transformClassesWithDexForDebug'解决方法
  • 原文地址:https://www.cnblogs.com/dzzy/p/8513328.html
Copyright © 2020-2023  润新知