题目
输入格式:
10
0 10 4 3 6 5 3 10 2 9
5
2 2
2 18
1 8 1
2 5
2 1
输出:
10
-1
5
0
思路
熟悉线性表插入和删除,搞清楚插入删除操作边界即可
代码
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
int n;
cin >> n;
ll* liner = new ll[n + 1000];
for(int i = 0; i < n; i++)
cin >> liner[i];
int q;
cin >> q;
for(int i = 0; i < q; i++)
{
int ope;
cin >> ope;
if(1 == ope)//insert
{
int loc;
int data;
cin >> loc >> data;
for(int i = n - 1; i > loc - 1; i--)
liner[i + 1] = liner[i];
n++;
liner[loc] = data;
}
else//delete
{
int loc;
cin >> loc;
if(loc <= n)
{
cout << liner[loc - 1] << endl;
for(int i = loc - 1; i < n - 1; i++)
{
liner[i] = liner[i + 1];
}
n--;
}
else
cout << "-1" << endl;
}
// cout << "case "<< i << endl;
// for(int j = 0; j < n; j++)
// cout << liner[j] << " ";
// cout << endl;
}
}