-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek3_Day1.java
More file actions
52 lines (42 loc) · 1.33 KB
/
Week3_Day1.java
File metadata and controls
52 lines (42 loc) · 1.33 KB
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
package Algorithm3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
public class Week3_Day1 {
public static int N, M;
public static int[] answerArr;
public static StringBuilder sb = new StringBuilder();
public static LinkedHashSet<String> hashset = new LinkedHashSet<String>();
// hashset은 전체 결과를 담는 Set
public static HashSet<Integer> visit = new HashSet<>();
public static String[] numbers;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] num = br.readLine().split(" ");
N = Integer.parseInt(num[0]);
M = Integer.parseInt(num[1]);
numbers = br.readLine().split(" ");
Arrays.sort(numbers);
dfs(0, "");
for(String i : hashset) {
System.out.println(i);
}
}
public static void dfs(int step, String str) {
if(step == M) {
hashset.add(str);
return ;
}
for(int i=0; i<N; i++) {
if(!visit.contains(i)) {
visit.add(i);
dfs(step+1,str+numbers[i]+" ");
visit.remove(i);
}
}
}
}