bupt-2015-2

第二题:LIST


描述:在该LIST上实现3种操作

         1append x在该LIST末尾添加xx32位整数

         2pop删除该LIST末尾的数

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

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

输出:输出find i找到的数


c++版:

#include<iostream>
#include<cstring>
using namespace std;
#define N 1005


int main(){
int m,d,i;
int a[N];
char op[15];
while(cin>>m){
int len=0;
while(m--){

cin>>op;
if(0==strcmp(op,"append")){
cin>>d;
a[len++]=d;
}else if(0==strcmp(op,"find")){
cin>>i;
if(i>=0){
cout<<a[i-1]<<endl;//应该-1,数组下标从0开始
}else{
cout<<a[len+i]<<endl;
}
}else{
len--;


}
}

}

}


c++版:博主葡萄家

#include<iostream>  
#include<cstring>  
#define maxn 1005  
using namespace std;  
int a[maxn];  
  
int main()  
{  
    int tes;  
  
    char tmp[15];  
    while(cin>>tes)  
    {  
        int m;  
        while(tes--)  
        {  
            cin>>m;  
            int len=0;  
            int d;  
            while(m--)  
            {  
                cin>>tmp;  
                if(strcmp(tmp,"append")==0)  
                {  
                    cin>>d;  
                    a[++len]=d;  
                }  
                else if(strcmp(tmp,"find")==0)  
                {  
                    cin>>d;  
                    if(d>0)  
                    {  
                        cout<<a[d]<<endl;  
                    }  
                    else  
                    {  
                        d=-d;  
                        cout<<a[len+1-d]<<endl;  
                    }  
                }  
                else  
                    len--;  
            }  
        }  
    }  
    return 0; 

}


C语言版:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 1000;
int main(){
int num,d,len,i;
int len1;
int a [1000];
char op [15];
while(scanf("%d",&num)!=EOF){
len=0;
while(num--){
scanf("%s",op);
if(0==strcmp(op,"append")){
scanf("%d",&d);
a[len++]=d;

}
//for(i=0;i<len;i++){此处不能用strlen(a)来判断数组a的长度
// printf("%d ",a[i]);}//要用strlen(a),a得是字符型数组
// printf("\n");
 
else if(0==strcmp(op,"find")){
scanf("%d",&d);
if(d>=0){
printf("%d",a[d-1]);
}else{
printf("%d",a[len+d]);
}
}else {
len--;
}
}
}
system("pause");
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值