利用栈实现逆序输出

【算法代码】

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

int n,x;
int main() {
    cin>>n;
    stack<int> s;

    while(n--) {
        cin>>x;
        s.push(x);
    }

    while(!s.empty()) {
        cout<<s.top()<<" ";
        s.pop();
    }

    return 0;
}


/*
in:
7
12 6 8 5 27 9 3

out:
3 9 27 5 8 6 12
*/

猜你喜欢

转载自blog.csdn.net/hnjzsyjyj/article/details/120588993