15663_N과 M (9) : 순열
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int N;
static int M;
static StringBuilder sb = new StringBuilder();
static LinkedHashSet<String> result = new LinkedHashSet<>();
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st1 = new StringTokenizer(br.readLine());
N = Integer.parseInt(st1.nextToken());
M = Integer.parseInt(st1.nextToken());
StringTokenizer st2 = new StringTokenizer(br.readLine());
int[] num = new int[N];
boolean[] checked = new boolean[N];
List<Integer> temp = new ArrayList<>();
for(int i=0; i<N; i++){
num[i] = Integer.parseInt(st2.nextToken());
}
Arrays.sort(num);
permutation(num, temp, checked);
Iterator<String> it = result.iterator();
while(it.hasNext()){
sb.append(it.next()).append('\n');
}
System.out.print(sb);
}
private static void permutation(int[] num, List<Integer> temp, boolean[] checked){
if(temp.size() == M){
String str = "";
for(int i=0; i<temp.size(); i++){
str += temp.get(i)+" ";
}
result.add(str);
return;
}
for(int i=0; i<num.length; i++){
if(!checked[i]){
checked[i] = true;
temp.add(num[i]);
permutation(num, temp, checked, result);
checked[i] = false;
temp.remove(temp.size()-1);
}
}
}
}
15664_N과 M (10) : 조합
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int N;
static int M;
static StringBuilder sb = new StringBuilder();
static LinkedHashSet<String> result = new LinkedHashSet<>();
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st1 = new StringTokenizer(br.readLine());
N = Integer.parseInt(st1.nextToken());
M = Integer.parseInt(st1.nextToken());
StringTokenizer st2 = new StringTokenizer(br.readLine());
int[] num = new int[N];
int[] temp = new int[M];
for(int i=0; i<N; i++){
num[i] = Integer.parseInt(st2.nextToken());
}
Arrays.sort(num);
combination(num, temp, 0, 0, 0);
Iterator<String> it = result.iterator();
while(it.hasNext()){
sb.append(it.next()).append('\n');
}
System.out.print(sb);
}
private static void combination(int[] num, int[] temp, int num_index, int temp_index, int cnt){
if(cnt == M){
String str = "";
for(int i=0; i<temp.length; i++){
str += temp[i]+" ";
}
result.add(str);
return;
}
if(num_index >= N)
return;
temp[temp_index] = num[num_index];
combination(num, temp, num_index+1, temp_index+1, cnt+1);
combination(num, temp, num_index+1, temp_index, cnt);
}
}
15665_N과 M (11) : 중복 순열
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int N;
static int M;
static StringBuilder sb = new StringBuilder();
static LinkedHashSet<String> result = new LinkedHashSet<>();
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st1 = new StringTokenizer(br.readLine());
N = Integer.parseInt(st1.nextToken());
M = Integer.parseInt(st1.nextToken());
StringTokenizer st2 = new StringTokenizer(br.readLine());
int[] num = new int[N];
List<Integer> temp = new ArrayList<>();
for(int i=0; i<N; i++){
num[i] = Integer.parseInt(st2.nextToken());
}
Arrays.sort(num);
permutation(num, temp);
Iterator<String> it = result.iterator();
while(it.hasNext()){
sb.append(it.next()).append('\n');
}
System.out.print(sb);
}
private static void permutation(int[] num, List<Integer> temp){
if(temp.size() == M){
String str = "";
for(int i=0; i<temp.size(); i++){
str += temp.get(i)+" ";
}
result.add(str);
return;
}
for(int i=0; i<num.length; i++){
temp.add(num[i]);
permutation(num, temp);
temp.remove(temp.size()-1);
}
}
}
15666_N과 M (12) : 중복 조합
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int N;
static int M;
static StringBuilder sb = new StringBuilder();
static LinkedHashSet<String> result = new LinkedHashSet<>();
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st1 = new StringTokenizer(br.readLine());
N = Integer.parseInt(st1.nextToken());
M = Integer.parseInt(st1.nextToken());
StringTokenizer st2 = new StringTokenizer(br.readLine());
int[] num = new int[N];
int[] temp = new int[M];
for(int i=0; i<N; i++){
num[i] = Integer.parseInt(st2.nextToken());
}
Arrays.sort(num);
combination(num, temp, 0, 0, 0);
Iterator<String> it = result.iterator();
while(it.hasNext()){
sb.append(it.next()).append('\n');
}
System.out.print(sb);
}
private static void combination(int[] num, int[] temp, int num_index, int temp_index, int cnt){
if(cnt == M){
String str = "";
for(int i=0; i<temp.length; i++){
str += temp[i]+" ";
}
result.add(str);
return;
}
if(num_index >= N)
return;
temp[temp_index] = num[num_index];
combination(num, temp, num_index, temp_index+1, cnt+1);
combination(num, temp, num_index+1, temp_index, cnt);
}
}
Set
- Set 컬렉션 클래스 : HashSet, TreeSet, LinkedHashSet
'1d-1c > BOJ' 카테고리의 다른 글
10026_적록색약 (JAVA) (0) | 2020.11.16 |
---|---|
10819_차이를 최대로 (JAVA) (0) | 2020.11.15 |
15654~15657_N과 M (5)~(8) (JAVA) (0) | 2020.11.11 |
15649~15652_N과 M (1)~(4) (JAVA) (0) | 2020.11.11 |
4963_섬의 개수 (JAVA) (C++) (0) | 2020.11.09 |