12.26 学校OJ题解

第一题:鸡蛋数列

其实就是用数组模拟队列操作。

AC代码如下:

#include <iostream>
#include <cctype>
using namespace std;

int main()
{
    int T;
    cin>>T;
    int a[105];
    while(T--){
        int n;
        cin>>n;
        int frontt=0,endd=0; //队首指针,队尾指针
        string str;
        int number;
        for(int i=0;i<n;i++){
            cin>>str; 
            if(str=="push"){ //压入队列
                cin>>number;
                a[endd++]=number; //队尾指针++
            }
            else frontt++; //否则就是pop 出队列 队首指针++
        }
        bool tag=false;
        if(frontt==endd){ //如果队首和队尾指针在同一位置说明没有鸡蛋了
            cout<<"no eggs!"<<endl;
        }
        else{
            for(int i=frontt;i<endd;i++){ //否则就输出
                if(tag)
                    cout<<' '<<a[i];
                else {
                    cout<<a[i];
                    tag=true;
                }
            }
            cout<<endl;
            }
        }

    return 0;
}

晚点补上

相关文章:

  • 2021-09-19
  • 2021-06-13
  • 2021-07-13
  • 2021-12-07
  • 2022-12-23
  • 2022-01-17
  • 2022-01-18
  • 2021-11-22
猜你喜欢
  • 2021-05-29
  • 2021-04-20
  • 2022-12-23
  • 2021-07-06
  • 2021-07-01
  • 2021-12-09
相关资源
相似解决方案