15654_N과 M (5) : 순열
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.List;
public class Main {
static int N;
static int M;
static StringBuilder sb = new StringBuilder();
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());
int[] num = new int[N];
boolean[] checked = new boolean[N];
List<Integer> ans = new ArrayList<>();
StringTokenizer st2 = new StringTokenizer(br.readLine());
for(int i=0; i<N; i++){
num[i] = Integer.parseInt(st2.nextToken());
}
Arrays.sort(num);
permutation(num, ans, checked);
System.out.print(sb);
}
private static void permutation(int[] num, List<Integer> ans, boolean[] checked){
if(ans.size()==M){
for(int i=0; i<ans.size(); i++){
sb.append(ans.get(i)).append(" ");
}
sb.append('\n');
return;
}
for(int i=0; i<num.length; i++){
if(!checked[i]){
checked[i] = true;
ans.add(num[i]);
permutation(num, ans, checked);
checked[i] = false;
ans.remove(ans.size()-1);
}
}
}
}
15655_N과 M (6) : 조합
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static int N;
static int M;
static StringBuilder sb = new StringBuilder();
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[] ans = new int[M];
for(int i=0; i<N; i++){
num[i] = Integer.parseInt(st2.nextToken());
}
Arrays.sort(num);
combination(num, ans, 0, 0, 0);
System.out.print(sb);
}
private static void combination(int[] num, int[] ans, int num_index, int ans_index, int cnt){
if(cnt == M){
for(int i=0; i<ans.length; i++){
sb.append(ans[i]).append(" ");
}
sb.append('\n');
return;
}
if(num_index >= N)
return;
ans[ans_index] = num[num_index];
combination(num, ans, num_index+1, ans_index+1, cnt+1);
combination(num, ans, num_index+1, ans_index, cnt);
}
}
15656_N과 M (7) : 중복 순열
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.List;
public class Main {
static int N;
static int M;
static StringBuilder sb = new StringBuilder();
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> ans = new ArrayList<>();
for(int i=0; i<N; i++){
num[i] = Integer.parseInt(st2.nextToken());
}
Arrays.sort(num);
permutation(num, ans);
System.out.print(sb);
}
private static void permutation(int[] num, List<Integer> ans){
if(ans.size() == M){
for(int i=0; i<ans.size(); i++){
sb.append(ans.get(i)).append(" ");
}
sb.append('\n');
return;
}
for(int i=0; i<num.length; i++){
ans.add(num[i]);
permutation(num, ans);
ans.remove(ans.size()-1);
}
}
}
15656_N과 M (8) : 중복 조합
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static int N;
static int M;
static StringBuilder sb = new StringBuilder();
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[] ans = new int[M];
for(int i=0; i<N; i++){
num[i] = Integer.parseInt(st2.nextToken());
}
Arrays.sort(num);
combination(num, ans, 0, 0, 0);
System.out.print(sb);
}
private static void combination(int[] num, int[] ans, int num_index, int ans_index, int cnt){
if(cnt == M){
for(int i=0; i<ans.length; i++){
sb.append(ans[i]).append(" ");
}
sb.append('\n');
return;
}
if(num_index >= N)
return;
ans[ans_index] = num[num_index];
combination(num, ans, num_index, ans_index+1, cnt+1);
combination(num, ans, num_index+1, ans_index, cnt);
}
}
'1d-1c > BOJ' 카테고리의 다른 글
10819_차이를 최대로 (JAVA) (0) | 2020.11.15 |
---|---|
15663~15666_ N과 M (9)~(12) (JAVA) (0) | 2020.11.13 |
15649~15652_N과 M (1)~(4) (JAVA) (0) | 2020.11.11 |
4963_섬의 개수 (JAVA) (C++) (0) | 2020.11.09 |
1182_부분수열의 합 (JAVA) (0) | 2020.11.08 |