怎么这题写的人这么少啊,不会都是杂❤鱼吧
这题使用C++的STL里的deque就能秒杀快加入C++大家族学习STL吧
如果是C语言或者不想用STL其实这题也能用循环数组写,应该叫这个名字吧
通过弄一个arr[200010],定义一个int变量head初始0,同样定义一个tail初始200010
最前方和最后方的插入和弹出通过head和tail实现就行了
#include <iostream>
#include<deque>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int q;
cin>>q;
deque<int> dq;
while(q--){
int n;
cin>>n;
if(n==1){
int x;
cin>>x;
dq.push_front(x);
}
else if(n==2){
int x;
cin>>x;
dq.push_back(x);
}
else if(n==3){
cout<<dq.front()<<"\n";
dq.pop_front();
}
else if(n==4){
cout<<dq.back()<<"\n";
dq.pop_back();
}
else{
cout<<"输入错误"<<"\n";
}
}
return 0;
}
// 64 位输出请用 printf("%lld")

京公网安备 11010502036488号