题解 | #【模板】栈#
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
#include <iostream>
#include <string>
using namespace std;
class mystack {
private:
int s[100000];
int index = 0;
public:
void push(int x) {
s[index] = x;
index++;
}
void pop() {
if(index>0){
index--;
cout << s[index] << endl;
}else{
cout<<"error"<<endl;
}
}
void top(){
if(index>0){
cout<<s[index-1]<<endl;
}else{
cout<<"error"<<endl;
}
}
};
int main() {
mystack ms;
int input_num;
cin>>input_num;
while(input_num){
string order;
cin>>order;
if(order=="push"){
int inp;
cin>>inp;
ms.push(inp);
}
else if(order=="pop"){
ms.pop();
}
else if(order=="top"){
ms.top();
}
input_num--;
}
return 0;
}
// 64 位输出请用 printf("%lld")
很基本的操作
上海得物信息集团有限公司公司福利 1161人发布
查看16道真题和解析