第一行输入一个N,表示栈中元素的个数第二行输入N个整数表示栈顶到栈底的各个元素
输出一行表示排序后的栈中栈顶到栈底的各个元素。
5 5 8 4 3 6
8 6 5 4 3
1 <= N <= 10000-1000000 <=<= 1000000
import sys class OrderHelpStack(object): def orderStack(self, stack): helpStack = [] while stack: stackTop = stack.pop() while helpStack and stackTop > helpStack[-1]: stack.append(helpStack.pop()) helpStack.append(stackTop) while helpStack: stack.append(helpStack.pop()) return stack N = int(sys.stdin.readline().strip()) stack = [] orderHelpStack = OrderHelpStack() line = sys.stdin.readline().strip() words = line.split() for word in words: stack.append(int(word)) stack = orderHelpStack.orderStack(stack) result = "" for i in range(N-1): result += str(stack.pop()) + " " result += str(stack.pop()) print(result)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i;
int n,x;
stack<int> s,help;
cin>>n;
vector<int> arr(n,0);
for(i=0;i<n;i++)
{
cin>>x;
arr.at(i)=x;
}
for(i=n-1;i>=0;i--)
s.push(arr.at(i));
while(!s.empty())
{
int temp=s.top();
s.pop();
while(!help.empty()&&help.top()>temp)
//这里一开始第二条件使用help.top()>=temp,结果
//超时严重,应该是测试数据有极端测试数据,导致程序
//交换过多导致的
{
s.push(help.top());
help.pop();
}
help.push(temp);
}
while(!help.empty())
{
if(help.size()==1) cout<<help.top();
else cout<<help.top()<<" ";
help.pop();
}
return 0;
} import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.LinkedList;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
br.readLine();
String[] secondLine = br.readLine().split(" ");
Deque<Integer> stack = new LinkedList<>();
Deque<Integer> help = new LinkedList<>();
for (String s : secondLine) {
stack.push(Integer.parseInt(s));
}
while (!stack.isEmpty()) {
int cur = stack.pop();
while (!help.isEmpty() && cur > help.peek()) {
stack.push(help.pop());
}
help.push(cur);
}
while (!help.isEmpty()) {
stack.push(help.pop());
}
StringBuilder res = new StringBuilder();
while (!stack.isEmpty()) {
res.append(stack.pop());
if (!stack.isEmpty()) {
res.append(" ");
}
}
System.out.println(res);
}
} import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Stack<Integer> stack1 = new Stack<>(), stack2 = new Stack<>();
int n = Integer.parseInt(br.readLine());
String[] strArr = br.readLine().split(" ");
for(int i = 0; i < n; i++)
stack1.push(Integer.parseInt(strArr[i]));
while(!stack1.isEmpty()){
int peekVal = stack1.pop();
if(stack2.isEmpty()){
stack2.push(peekVal);
}else{
if(peekVal >= stack2.peek()){
stack2.push(peekVal);
}else{
while(!stack2.isEmpty() && peekVal < stack2.peek())
stack1.push(stack2.pop());
stack2.push(peekVal);
}
}
}
while(!stack2.isEmpty())
System.out.print(stack2.pop() + " ");
}
} import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void sortStackByStack(Stack<Integer> stack) {
Stack<Integer> help = new Stack<>();
while (!stack.isEmpty()) {
int temp = stack.pop();
while (!help.isEmpty() && help.peek() > temp) {
stack.push(help.pop());
}
help.push(temp);
}
while (!help.isEmpty()) {
int temp = help.pop();
stack.push(temp);
System.out.print(temp + " ");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Stack<Integer> stack = new Stack<>();
while (n-- > 0) {
stack.push(sc.nextInt());
}
sortStackByStack(stack);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Main {
public static void sortStackByStack(Stack<Integer> stack) {
Stack<Integer> help = new Stack<>();
while (!stack.isEmpty()) {
int cur = stack.pop();
while (!help.isEmpty() && help.peek() < cur) {
stack.push(help.pop());
}
help.push(cur);
}
while (!help.isEmpty()) {
stack.push(help.pop());
}
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Stack<Integer> helpInput = new Stack<>();
int n = Integer.parseInt(in.readLine());
String[] stackArray = in.readLine().split(" ");
for (int i = n-1;i >=0;i--){
helpInput.push(Integer.parseInt(stackArray[i]));
}
sortStackByStack(helpInput);
while (!helpInput.isEmpty()){
System.out.print(helpInput.pop()+ " ");
}
in.close();
}
}
import java.util.*;
public class Main{
public static void sortStack(Stack<Integer> stack) {
Stack<Integer> help = new Stack<>();
while (!stack.isEmpty()) {
int cur = stack.pop();
while (!help.isEmpty() && cur > help.peek()) {
stack.push(help.pop());
}
help.push(cur);
}
while (!help.isEmpty()) {
stack.push(help.pop());
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = n - 1; i >= 0; i--) {
arr[i] = scanner.nextInt();
}
// for (int i = 0; i < n; i++) { // 错误示范: case通过率为95.24%
// arr[i] = scanner.nextInt();
// }
Stack<Integer> stack = new Stack();
for (int item : arr) {
stack.push(item);
}
sortStack(stack);
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
}
} #include <bits/stdc++.h>
using namespace std;
int main(){
stack<int> S1, S2;
int n, x, t;
cin>>n;
for(int i=0;i<n;i++){
cin>>x;
S1.push(x);
}
while(!S1.empty()){
x = S1.top();
S1.pop();
if(S2.empty())
S2.push(x);
else{
t = S2.top();
while(!S2.empty() && t>x){
S2.pop();
S1.push(t);
t = S2.top();
}
S2.push(x);
}
}
while(!S2.empty()){
cout<<S2.top()<<" ";
S2.pop();
}
return 0;
} const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("line", function (line) {
const tokens = line.split(" ");
let n;
if (tokens.length < 2 && n == undefined) {
n = Number(tokens[0]);
return;
}
const nums = tokens.map(Number);
let st1 = [];
let st2 = [];
while (nums.length) {
st1.push(nums.pop());
}
let min;
const result = [];
while (st1.length || st2.length) {
if (st1.length) {
while (st1.length) {
const top = st1.pop();
if (min == undefined) {
min = top;
continue;
}
if (min > top) {
st2.push(min);
min = top;
} else {
st2.push(top);
}
}
} else {
while (st2.length) {
const top = st2.pop();
if (min == undefined) {
min = top;
continue;
}
if (min > top) {
st1.push(min);
min = top;
} else {
st1.push(top);
}
}
}
result.push(min);
min = undefined
}
console.log(result.reverse().reduce((a, c) => {
a += ' ' + c
return a
}))
});
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main() {
int n;
cin>>n;
vector<int> input(n);
stack<int> st;
for(int i=0;i<n;i++){
cin>>input[i];
}
for(int i=n-1;i>=0;i--)
st.push(input[i]);
stack<int> help;
for(int i=0;i<n;i++){
int cur = st.top();
st.pop();
int cnt = 0;
while(!help.empty() && help.top()<cur){
st.push(help.top());
help.pop();
cnt ++;
}
help.push(cur);
while(cnt--){
help.push(st.top());
st.pop();
}
}
while(!help.empty()){
st.push(help.top());
help.pop();
}
while(!st.empty()){
cout<<st.top()<<" ";
st.pop();
}
} package main
import (
"fmt"
)
func main() {
num := 0
fmt.Scan(&num)
s := Stack{}
for i := 0; i < num; i++ {
v := 0
fmt.Scan(&v)
s.Push(v)
}
sortStack(&s)
for !s.IsEmpty() {
fmt.Print(s.Pop())
fmt.Print(" ")
}
fmt.Println("")
}
func sortStack(s *Stack) {
t := Stack{}
for !s.IsEmpty() {
top := s.Pop()
for !t.IsEmpty() && top > t.Top() {
s.Push(t.Pop())
}
t.Push(top)
}
for !t.IsEmpty() {
s.Push(t.Pop())
}
}
type Stack struct {
data []int
}
func (s *Stack) Push(v int) {
s.data = append(s.data, v)
}
func (s *Stack) Pop() int {
top := s.data[len(s.data)-1]
s.data = s.data[:len(s.data)-1]
return top
}
func (s *Stack) IsEmpty() bool {
return len(s.data) == 0
}
func (s *Stack) Top() int {
return s.data[len(s.data)-1]
}
import sys # 处理输入 N = sys.stdin.readline().strip() stack = [int(i) for i in sys.stdin.readline().strip().split()] # 使用辅助栈,基于汉诺塔原理实现栈的排序 def sortStack(stack): helper = [] while stack: val = stack.pop() if not helper: helper.append(val) else: while helper and val > helper[-1]: stack.append(helper.pop()) helper.append(val) while helper: stack.append(helper.pop()) return stack stack = sortStack(stack) while stack: print(stack.pop(), end=' ')
#include <bits/stdc++.h>
using namespace std;
stack<int> stOut; // 输出栈
void push(stack<int>& stIn)
{
while(!stIn.empty()){
int tmp = stIn.top();
stIn.pop();
// 输出栈顶元素比当前元素大,就不断 pop 出来,重新插入输入栈
while(!stOut.empty() && tmp < stOut.top()){
int top = stOut.top();
stOut.pop();
stIn.push(top);
}
// 否则就插入输出栈
stOut.push(tmp);
}
}
int main()
{
int n;
cin >> n;
vector<int> nums(n);
for(int i = 0; i < n; ++i) cin >> nums[i];
stack<int> stIn; // 输入栈
for(int i = 0; i < n; ++i){
stIn.push(nums[i]);
}
push(stIn);
while(!stOut.empty()){
cout << stOut.top() << " " ;
stOut.pop();
}
return 0;
} import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Stack<Integer> stackA = new Stack<Integer>();
Stack<Integer> stackB = new Stack<Integer>();
while(sc.hasNextInt()){
stackA.push(sc.nextInt());
}
while(!stackA.isEmpty()){
int temp=stackA.pop();
while( !stackB.isEmpty() && stackB.peek()>temp){
stackA.push(stackB.pop());
}
stackB.push(temp);
}
while(!stackB.isEmpty()){
System.out.print(stackB.pop()+" ");
}
}
} # 输入的元素个数 n = int(input()) # 用列表模仿栈 stack = [] number = input().split() number.reverse() for i in range(len(number)): stack.append(int(number[i])) # 辅助的栈 help = [] while len(stack) != 0: a = stack.pop() while len(help) != 0 and help[-1] < a: stack.append(help.pop()) help.append(a) while len(help) != 0: stack.append(help.pop()) # 输出栈顶到栈底 for i in range(len(stack)): print(stack.pop(), end=' ')《程序员代码面试》Python题解 https://zhuanlan.zhihu.com/p/444550262
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Stack;
/**
* @author hugh
* @create 2021-09-04 23:10
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
String[] line = in.readLine().split(" ");
Stack<Integer> data = new Stack<>();
for (int i = n - 1; i >= 0; i--) {
data.push(Integer.parseInt(line[i]));
}
Stack<Integer> stack = new Stack<>();
sort(data, stack);
StringBuilder res = new StringBuilder();
while (!data.isEmpty()) {
res.append(data.pop() + " ");
}
System.out.println(res.substring(0, res.length() - 1));
}
public static void sort(Stack<Integer> data, Stack<Integer> stack) {
while (!data.isEmpty()) {
Integer num1 = data.pop();
put(stack, num1);
}
while (!stack.isEmpty()) {
data.push(stack.pop());
}
}
/**
* 将x插入data中的合适位置,保证从大到小的顺序
* @param stack 按照从大到小(栈底->栈顶)排序的栈
* @param x 整数
*/
public static void put(Stack<Integer> stack, Integer x) {
if (stack.isEmpty() || stack.peek() > x) {
stack.push(x);
} else {
Integer num = stack.pop();
put(stack, x);
stack.push(num);
}
}
}
#include<iostream>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int N,x;
cin>>N;
stack<int> stack1,stack2;
for(int i=0;i<N;++i){
cin>>x;
stack1.push(x);
}
while(!stack1.empty()){
int cur=stack1.top();
stack1.pop();
while(!stack2.empty() && cur>stack2.top()){
stack1.push(stack2.top());
stack2.pop();
}
stack2.push(cur);
}
while(!stack2.empty()){
stack1.push(stack2.top());
stack2.pop();
}
for(int i=0;i<N;++i){
cout<<stack1.top()<<" ";
stack1.pop();
}
} 十一点吃个🍎 👉🏻map竟然忘记返值
let line=readline()
let stack=[],help=[]
while(line=readline()){
stack=line.split(' ')
stack= stack.map(item=>parseInt(item))
// console.log(stack)
while(stack.length){
let cur=stack.pop()
if(help.length==0||help[help.length-1]<cur){
help.push(cur)
}else{
while(help.length){
var helpcur=help[help.length-1]
if(helpcur>cur){
stack.push(help.pop())
}else break
}
help.push(cur)
}
}
console.log(help.reverse().join(' '))
}