最多用一个栈进行排序,最小的位于栈顶
#include <iostream>
#include <cstdio>
#include <stack>//直接引用头文件 内含栈的定义
using namespace std;
stack<int> b;
stack<int> c;
void shuru(stack<int> &b)
{
int n;
scanf("%d",&n);
while(n--)
{
int a;
cin>>a;
b.push(a);
}
}
void sunjian(stack<int> &b)
{
if(b.empty()) return;
while(!b.empty())
{
int x=b.top();
b.pop();
while(!c.empty()&&x>c.top())
{
b.push(c.top());
printf("b.push(%d)\n",c.top());
c.pop();
}
c.push(x);
printf("c.push(%d)\n",x);
}
}
int main()
{
while(1)
{
//memset(b,b.length());
shuru(b);
sunjian(b);
while(!c.empty())
{
cout<<c.top()<<" ";
c.pop();
}
cout<<endl;
}
}