【问题描述】
有n个物品,编号为1,2,....,n
有这样一个机器,你按1它就会从左到右,把一个物品抓起放到桶的顶端(如果没有物品可以抓取,就不操作);
你按2就会把桶顶端的物品抓出来进行加工,如果桶里什么都没有的话,就不操作。
【输入形式】
第一行输入物品个数n和操作次数m
第二行输入加工的编号
【输出形式】
输出加工过的物品的编号
【样例输入】
4 5
1 1 2 1 2
【样例输出】
2 3
【题解代码】
#include <iostream>
using namespace std;
class Stack
{
public:
int *data;
int size;
int top;
Stack(int s)
{
size=s;
top=-1;
data=new int[size];
}
~Stack()
{
delete [] data;
}
void push(int x)
{
data[++top]=x;
}
void pop()
{
if(!isEmpty()){
cout<<data[top];
top--;
}
else{
//cout<<"栈为空"<<endl;
return ;
}
}
bool isEmpty()
{
if(top==-1)
return true;
else
return false;
}
int getTop()
{
return data[top];
}
};
int main() {
int n, m,optr;
cin >>n>>m;
Stack p(n);
int num=0;
for(int i=0;i<m;i++)
{
cin>>optr;
if(optr==1&&num<n)
{
p.push(num+1);
num++;
}
else if(optr==2&&!p.isEmpty())
{
p.pop();
cout<<" ";
}
}
return 0;
}