728x90
https://www.acmicpc.net/problem/15663
15663번: N과 M (9)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
// 코드
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
54
55
56
57
58
59
60
61
|
import java.util.*;
import java.io.*;
public class Main{
static StringBuilder resSb = new StringBuilder();
static int N, M;
static int[] arr;
static int[] result;
static boolean[] visited;
// 중복을 체크한다.
static HashSet<String> set = new HashSet<>();
static void getResult(int x) {
if(x >= M) {
StringBuilder sb = new StringBuilder();
for(int i=0; i<result.length; i++) {
sb.append(result[i] + " ");
}
if(!set.contains(sb.toString())) {
resSb.append(sb.toString() + "\n");
set.add(sb.toString());
}
return;
}
for(int i=0; i<N; i++) {
if(!visited[i]) {
visited[i] = true;
result[x] = arr[i];
getResult(x+1);
visited[i] = false;
}
}
}
// main
public static void main(String[] args) throws Exception {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
result = new int[M];
visited = new boolean[N];
arr = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
Arrays.sort(arr);
getResult(0);
bw.write(resSb.toString());
br.close();
bw.flush();
bw.close();
}
}
|
cs |
반응형