[알고리즘] 백준 - 15651 ( N과 M (7) ) / 자바

배고픈메꾸리·2021년 4월 9일
0

알고리즘

목록 보기
75/128
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
	static int N, M;
	static int[] map;
	static int[] result;
	static StringBuilder sb = new StringBuilder();

	public static void main(String[] args) throws IOException{
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		N = Integer.parseInt(st.nextToken());
		M = Integer.parseInt(st.nextToken());
		
		map = new int[N];
		result = new int[M];
		
		st=  new StringTokenizer(br.readLine());
		for (int i = 0; i < N; i++) {
			map[i] = Integer.parseInt(st.nextToken());
		}
			
		Arrays.sort(map);
		bt(0);
		System.out.print(sb);
	}

	static void bt(int depth) {
		if (depth == M) {
			for (int i : result)
				sb.append(i + " ");
			sb.append("\n");
			return;
		}
		for (int i = 0; i < N; i++) {
			result[depth] = map[i];
			bt(depth + 1);
		}
	}
}

profile
FE 개발자가 되자

0개의 댓글