[백준 - 15654] N과 M (5)

[백준 - 15654] N과 M (5)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

package com.example.study.algorithm.baekjoon; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class BaeckJoon_15654 { static int [] input; static int [] output; static boolean [] check; public static void main( String [] args) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader( System . in )); StringTokenizer st = new StringTokenizer(br.readLine()); int N = Integer. parseInt (st.nextToken()); int M = Integer. parseInt (st.nextToken()); input = new int [N]; output = new int [M]; check = new boolean [N]; st = new StringTokenizer(br.readLine()); for ( int i = 0 ; i < N; i + + ) { input[i] = Integer. parseInt (st.nextToken()); } Arrays.sort(input); permu( 0 , M, N); } public static void permu( int depth, int m, int n) { if (depth = = m) { for ( int i : output) { System . out . print (i + " " ); } System . out . println ( "" ); return ; } for ( int i = 0 ; i < n; i + + ) { if (check[i] = = false ) { check[i] = true ; output[depth] = input[i]; permu(depth + 1 , m, n); check[i] = false ; } } } } Colored by Color Scripter

from http://algorithm-master.tistory.com/146 by ccl(A) rewrite - 2021-12-04 16:28:08