请先学习下知识点,道友!
题目知识点大部分来源于此:代码随想录:栈与队列
题目思路:
题目代码:
//注意:stack的push() top() pop() queue 的push() front() pop();
//对于N要进行适应性的更改,对于字段错误
//if(sc[x].size()>0){
#include
using namespace std;
#define inf 0x3f3f3f3f
#define N 1001
int n,m,k,g,d;
int x,y,z;
char ch;
string str;
vector<int>v[N];
queue<char>sc[N];
int main() {
cin>>n>>m>>d;
//注意铁轨上的用queue储存。
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>ch;
sc[i].push(ch);
}
}
stack<char>s;
while(cin>>k){
if(k==-1)break;
if(k==0){
//抓取时,判断是否有
if(s.size()>0){
cout<<s.top();
s.pop();
}
}else{
//从铁轨推东西时看是否还有东西,以及容器里是否满了。
if(sc[k].size()>0){
if(s.size()==d){
cout<<s.top();
s.pop();
}
s.push(sc[k].front());
sc[k].pop();
}
}
}
return 0;
}
题目思路:
题目代码:
//记住stack' 没有 s.clear()
//记住要先判断s的非空才能使用s.top();;
// while(!s.empty()&&cur==s.top()){
#include
using namespace std;
int n,m,k;
int x,y,z;
int main(){
cin>>n>>m>>k;
while(k--){
int cur=1; //用来记录彩虹瓶当前需要填装的货号
stack<int>s; //这个stack表示临时货架
for(int i=1;i<=n;i++){
cin>>x;
if(x==cur){
cur++;
while(!s.empty() &&s.top()==cur){//必须加上判断s非空
s.pop();
cur++;
}
}else{
if(s.size()<m)s.push(x);
}
}
if(cur==n+1)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
题目思路:
题目代码:
//这里面是num2 op num1
//对于N要进行适应性的更改,对于字段错误
#include
using namespace std;
#define inf 0x3f3f3f3f
#define N 100100
int n,m,k,g,d;
int x,y,z;
char ch;
string str;
vector<int>v[N];
int main() {
stack<int>s1;
stack<char>s2;
cin>>n;
for(int i=0;i<n;i++){
cin>>x;
s1.push(x);
}
for(int i=0;i<n-1;i++){
cin>>ch;
s2.push(ch);
}
int flag=0;
while(!s2.empty()){
int num1=s1.top();
s1.pop();
int num2=s1.top();
s1.pop();
ch=s2.top();
s2.pop();
if(ch=='+'){
s1.push(num2+num1);
}else if(ch=='-'){
s1.push(num2-num1);
}else if(ch=='*'){
s1.push(num2*num1);
}else if(ch=='/'){
if(num1==0){
cout<<"ERROR: "<<num2<<"/0"<<endl;
flag=1;
break;
}else{
s1.push(num2/num1);
}
}
}
if(flag==0)cout<<s1.top();
return 0;
}
题目思路:
题目代码:
//实现vector 插入时进行排序;;
//核心代码:
cin>>x;
it=lower_bound(v.begin(),v.end(),x);
v.insert(it,x);
//“取中值”——即返回所有堆栈中元素键值的中值。 并不进行跳出好像
//对于N要进行适应性的更改,对于字段错误
#include
using namespace std;
#define inf 0x3f3f3f3f
#define N 100100
int n,m,k,g,d;
int x,y,z;
char ch;
string str;
vector<int>v[N];
int main()
{
vector<int> ans;
vector<int>order;
vector<int>::iterator it;
cin>>n;
while(n--){
cin>>str;
if(str[1]=='u'){
cin>>x;
ans.push_back(x);
it=lower_bound(order.begin(),order.end(),x);
order.insert(it,x);
}else if(str[1]=='e'){
if(order.size()>0){
int num=order.size();
cout<<order[(num-1)/2]<<endl;
}else{
cout<<"Invalid"<<endl;
}
}else if(str[1]=='o'){
if(order.size()>0){
int num=ans[ans.size()-1];
cout<<num<<endl;
ans.pop_back();
it=lower_bound(order.begin(),order.end(),num);
order.erase(it);
}else{
cout<<"Invalid"<<endl;
}
}
}
}
//超时的
#include
using namespace std;
#define N 100100
#define inf 0x3f3f3f3f
int n,m,d,k;
int x,y,z;
string str;
char ch;
vector<int>v[N];
int main()
{
cin>>n;
stack<int>s;
vector<int>cur;
vector<int>::iterator it;
while(n--){
cin>>str;
if(str=="Pop"){
if(s.size()==0){
cout<<"Invalid"<<endl;
}else{
int num=s.top();
s.pop();
cout<<num<<endl;
it=lower_bound(cur.begin(),cur.end(),num);
cur.erase(it);
}
}else if(str=="PeekMedian"){
if(cur.size()>0){
sort(cur.begin(),cur.end());
int num=cur.size();
cout<<cur[(num-1)/2]<<endl;
}else{
cout<<"Invalid"<<endl;
}
}else if(str=="Push"){
cin>>x;
cur.push_back(x);
s.push(x);
}
}
return 0;
}