13 用一个栈实现另一个栈的排序
Table of Contents

ac(两个栈模拟排序即可)

import java.util.*;

public class Main {

    public static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {

        int n = sc.nextInt();
        Stack<Integer> stack = new Stack<>();
        int[] arr = new int[n];
        for(int i=0;i<n;i++){
            int val = sc.nextInt();
            arr[i] = val;
        }
        for(int i=n-1;i>=0;i--){
            stack.push(arr[i]);
        }
        Stack<Integer> help = new Stack<>();
        while (!stack.isEmpty()){
            int topVal = stack.pop();
            if(help.isEmpty()){
                help.push(topVal);
            }else {
                int cnt = 0;
                while (!help.isEmpty()){
                    int helpTop = help.peek();
                    if(topVal < helpTop){
                        help.pop();
                        stack.push(helpTop);
                        cnt++;
                    }else {
                        break;
                    }
                }
                help.push(topVal);
                for(int i=0;i<cnt;i++){
                    help.push(stack.pop());
                }
            }
        }
        boolean flag = true;
        while (!help.isEmpty()){
            if(flag) {
                System.out.print(help.pop());
                flag = false;
            }else {
                System.out.print(" " + help.pop());
            }
        }
        System.out.println();
    }

}