17. 打饭队列

小张经常会在打饭排队的时候由于无聊而东张西望。今天他对隔壁的打饭队伍进行了详细观察。隔壁队伍每次到达一名同学,他就会记录下这个同学的名字,同时队伍的第一个同学打饭完成后会离开队伍。同时小张有时候想关注一下队首或队尾的同学是谁。

小张正在忙着排队打饭,请你帮助他完成这一件任务。

解题思路:

队列模拟即可,利用stl或者手写队列均可,坑点是可能会leave一个空队列,但是题里没告诉qwq,记得排除这个,否则喜提re或者wa

代码:

#include<iostream>  
#include<queue>  
using namespace std;  
queue<string> q;  
int main(){  
    int t;  
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);  
    cin>>t;  
    while(t--){  
        char c[11];  
        cin>>c;  
        if(c[0]=='A'){  
            string s;  
            cin>>s;  
            q.push(s);  
        }  
        else if(c[0]=='L'){  
            if(!q.empty())q.pop();  
            }  
        else if(c[8]=='d'){  
            if(!q.empty())cout<<q.front()<<endl;  
            else cout<<"Empty queue"<<endl;  
        }  
        else {  
            if(!q.empty())cout<<q.back()<<endl;  
            else cout<<"Empty queue"<<endl;  
        }  
    }  
} 

数组模拟版(当时忘判空了)

#include<iostream> 
using namespace std; 
string l[2000010]; 
int hh,tt=-1; 
int idx; 
int main(){ 
    int t; 
 cin>>t; 
  while(t--){ 
        char c[10]; 
        cin>>c; 
      if(c[0]=='A'){ 
         string s; 
          cin>>s; 
          l[++tt]=s; 
     } 
      else if(c[0]=='L'){ 
            if(hh<=tt)hh++; 
      } 
      else if(c[8]=='d'){ 
            if(hh<=tt)cout<<l[hh]<<endl; 
            else puts("Empty queue"); 
      } 
      else { 
         if(hh<=tt)cout<<l[tt]<<endl; 
            else puts("Empty queue"); 
      } 
  } 
} 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值