题解 | #用一个栈实现另一个栈的排序#
用一个栈实现另一个栈的排序
http://www.nowcoder.com/practice/ff8cba64e7894c5582deafa54cca8ff2
#include<bits/stdc++.h>
using namespace std;
stack<int> S1,S2;
int main(){
int N;
cin>>N;
for(int i=0;i<N;i++){
int x;
cin>>x;
while(!S1.empty()&&x<S1.top()){//当栈1不为空且x小于栈顶元素时
S2.push(S1.top());
S1.pop();
}
S1.push(x);
while(!S2.empty()){
S1.push(S2.top());
S2.pop();
}
}
while(!S1.empty()){
cout<<S1.top()<<" ";
S1.pop();
}
}
using namespace std;
stack<int> S1,S2;
int main(){
int N;
cin>>N;
for(int i=0;i<N;i++){
int x;
cin>>x;
while(!S1.empty()&&x<S1.top()){//当栈1不为空且x小于栈顶元素时
S2.push(S1.top());
S1.pop();
}
S1.push(x);
while(!S2.empty()){
S1.push(S2.top());
S2.pop();
}
}
while(!S1.empty()){
cout<<S1.top()<<" ";
S1.pop();
}
}