题解 | #火车进站#

火车进站

https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109

#include <iostream>
#include<stack>
#include <algorithm>
using namespace std;
//十分死板的模拟火车近战出站,空间耗麻了
string tres[10000];
int i=0;

void outInto(stack<int> in,stack<int> out,stack<int> res){
    if(in.empty()== true){
        while(!out.empty()){
            res.push(out.top());
            out.pop();
        }
        while(!res.empty()){
            tres[i]=to_string(res.top())+' '+tres[i];
            res.pop();
        }
        i++;
        return;
    }
    if(out.empty()==true){        
        out.push(in.top());        
        in.pop();
        outInto(in,out,res);        
    }
    else 
    {
        stack<int> temp=out;
        stack<int> temp2=res; 
        res.push(out.top());          
        out.pop();
        outInto(in,out,res);
        out=temp;
        res=temp2;
        out.push(in.top());        
        in.pop();
        outInto(in,out,res);
    }

    return;


}


int main() {
    int n, N;
    stack<int> in;
    stack<int> out;
    stack<int> res;
    cin>>n;
    while (cin >> N) { // 注意 while 处理多个 case
        out.push(N);
    }
    while(n--){
        in.push(out.top());
        out.pop();
    }
 
    outInto(in, out,res);
    sort(tres,tres+i);
    for(int j=0;j<i;j++){
        cout<<tres[j]<<endl;
    }
    return 0;

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

练习JAVA时长两年半:qps 30000
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务