4536: 【例】栈的存入与遍历

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:17 解决:12

题目描述

样例输入 复制

10 20 30 40 50 60 -1

样例输出 复制

60
50
40
30
20
10

提示

#include<bits/stdc++.h>
using namespace std;

int main(){
    //stack<int>s;
    //s.push(10);
    //s.push(20);
    //s.push(30);
    //cout<<s.top()<<endl;
    //s.pop();
    //出栈
    //cout<<s.top()<<endl;

    //向栈中存入元素,直到遇到-1结束 
    stack<int> s;
    int x;
    while(1==1){
        cin>>x;
        if(x==-1) break;
        s.push(x);
    }
    //将元素逐个访问(将元素逐个弹出)
    while(s.empty()== false){
        cout<<s.top()<<endl;//获取栈顶元素
        s.pop(); //弹出栈顶元素
    }
    return 0;
}