이전 순열

|


import java.util.Scanner;
import java.util.Stack;

public class BOJ_10973 {

    static int[] arr;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        arr = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        prevPermutation();
    }

    private static void prevPermutation() {
        int idx = arr.length - 1;

        while (true) {

            if (idx == 0) {
                System.out.println(-1);
                break;
            }

            if (arr[idx - 1] < arr[idx]) {
                idx -= 1;
                continue;
            }

            int i = idx;
            int j = -1;

            for (int k = i - 1; k < arr.length; k++) {
                if (arr[i - 1] > arr[k]) {
                    j = k;
                }
            }

            if (j != -1) {
                swap(i - 1, j);
                reverse(i);
                print();
                break;
            }


        }

    }

    private static void print() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]);
            if (i != arr.length - 1)
                sb.append(" ");
        }
        System.out.println(sb.toString());
    }

    private static void reverse(int i) {
        Stack<Integer> stack = new Stack<>();
        for (int j = i; j < arr.length; j++) {
            stack.add(arr[j]);
        }
        for (int j = i; j < arr.length; j++) {
            arr[j] = stack.pop();
        }
    }

    private static void swap(int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }


}

다음 순열

|


import java.util.Scanner;
import java.util.Stack;

public class BOJ_10972 {

    static int[] arr;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        arr = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        nextPermutation();
    }

    private static void nextPermutation() {

        int idx = arr.length - 1;

        while (true) {

            if (idx == 0) {
                System.out.println(-1);
                break;
            }

            if (arr[idx - 1] > arr[idx]) {
                idx -= 1;
                continue;
            }

            int i = idx;
            int j = -1;
            for (int k = idx - 1; k < arr.length; k++) {
                if (arr[idx - 1] < arr[k])
                    j = k;
            }

            if (j != -1) {
                swap(i - 1, j);
                reverse(i);
                print();
                break;
            }
        }
    }


    private static void print() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]);
            if (i != arr.length - 1)
                sb.append(" ");
        }
        System.out.println(sb.toString());
    }

    private static void reverse(int idx) {
        Stack<Integer> stack = new Stack<>();
        for (int i = idx; i < arr.length; i++) {
            stack.add(arr[i]);
        }
        for (int i = idx; i < arr.length; i++) {
            arr[i] = stack.pop();
        }
    }

    private static void swap(int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}


N과 M(4)

|


import java.util.Scanner;

public class BOJ_15652 {

    static int[] arr = new int[8];

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();

        go(0, n, m);
    }

    private static void go(int index, int n, int m) {

        if (index == m) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < m; i++) {
                sb.append(arr[i]);
                sb.append(" ");
            }
            sb.append("\n");
            System.out.print(sb.toString());
            return;
        }

        for (int i = 1; i <= n; i++) {
            if(index > 0){
                if(arr[index-1] > i)
                    continue;
            }
            arr[index] = i;
            go(index + 1, n, m);
        }
    }
}

N과 M(3)

|


import java.util.Scanner;

public class BOJ_15651 {


    static int[] arr = new int[8];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();

        go(0, n, m);

    }

    private static void go(int index, int n, int m) {
        if (index == m) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < m; i++) {
                sb.append(arr[i]);
                sb.append(" ");
            }
            sb.append("\n");
            System.out.print(sb.toString());
            return;
        }

        for (int i = 1; i <= n; i++) {
            arr[index] = i;
            go(index + 1, n, m);
        }
    }
}

N과 M(2)

|


import java.util.Scanner;

public class BOJ_15650 {

    static boolean[] check = new boolean[9];
    static int[] arr = new int[9];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        go(0, 1, n, m);
    }

    private static void go(int index, int start, int n, int m) {
        if (index == m) {
            for (int i = 0; i < index; i++) {
                System.out.print(arr[i]);
                if (i < m)
                    System.out.print(" ");
            }
            System.out.println();
            return;
        }

        for (int i = start; i <= n; i++) {
            if (check[i])
                continue;
            check[i] = true;
            arr[index] = i;
            go(index + 1, i + 1, n, m);
            check[i] = false;
        }
    }
}