-
[백준] N과 M (시리즈)ALGORITHM/BOJ 2021. 4. 21. 21:30
www.acmicpc.net/workbook/view/2052
2021-04-21
1. N과 M (1) :: 15649
1234567891011121314151617181920212223242526272829303132333435363738import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main15649_NM1 {public static int N, M;public static int[] arr;public static boolean[] vtd;public static void solve(int cnt) {if(cnt == M) {for(int i = 0; i < M; i++) System.out.print(arr[i] + " ");System.out.println();return;}for(int i = 0; i < N; i++) {if(!vtd[i]) {vtd[i] = true;arr[cnt] = (i+1);solve(cnt+1);vtd[i] = false;}}}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());M = Integer.parseInt(st.nextToken());arr = new int[M];vtd = new boolean[N];solve( 0);}}cs 2. N과 M (2) :: 15650
123456789101112131415161718192021222324252627282930313233343536373839import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main15650_NM2 {public static int N, M;public static int[] arr;public static boolean[] vtd;public static void solve(int idx, int cnt) {if(cnt == M) {for(int i = 0; i < M; i++) System.out.print(arr[i] + " ");System.out.println();return;}for(int i = idx; i < N; i++) {if(!vtd[i]) {vtd[i] = true;arr[cnt] = (i+1);solve(i, cnt+1);vtd[i] = false;}}}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());M = Integer.parseInt(st.nextToken());arr = new int[M];vtd = new boolean[N];solve(0, 0);}}cs 3. N과 M (3) :: 15651
123456789101112131415161718192021222324252627282930313233343536import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main15651_NM3 {public static int N, M;public static int[] arr;public static StringBuilder sb;public static void solve(int cnt) {if(cnt == M) {for(int i = 0; i < M; i++) sb.append(arr[i]).append(" ");sb.append("\n");return;}for(int i = 0; i < N; i++) {arr[cnt] = (i+1);solve(cnt+1);}}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());M = Integer.parseInt(st.nextToken());sb = new StringBuilder();arr = new int[M];solve(0);System.out.println(sb.toString());}}cs 4. N과 M (4) :: 15652
12345678910111213141516171819202122232425262728293031323334import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main15652_NM4 {public static int N, M;public static int[] arr;public static StringBuilder sb;public static void solve(int idx, int cnt) {if(cnt == M) {for(int i: arr) sb.append(i + " ");sb.append("\n");return;}for(int i = idx; i < N; i++) {arr[cnt] = (i+1);solve(i, cnt+1);}}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());M = Integer.parseInt(st.nextToken());arr = new int[M];sb = new StringBuilder();solve(0, 0);System.out.println(sb.toString());}}cs 5. N과 M (5) :: 15654
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Arrays;import java.util.StringTokenizer;public class Main15654_NM5 {public static int N, M;public static int[] arr, order;public static boolean[] vtd;public static StringBuilder sb;public static void solve( int cnt) {if(cnt == M) {for(int value: order) sb.append(arr[value] + " ");sb.append("\n");return;}for(int i = 0; i < arr.length; i++) {if(!vtd[i]) {vtd[i] = true;order[cnt] = i;solve(cnt+1);vtd[i] = false;}}}public static void main(String[] args) throws Exception {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());M = Integer.parseInt(st.nextToken());arr = new int[N];vtd = new boolean[N];order = new int[M];sb = new StringBuilder();st = new StringTokenizer(bf.readLine());for(int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken());Arrays.sort(arr);solve(0);System.out.println(sb.toString());}}cs 6. N과 M (6) :: 15655
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;import java.util.StringTokenizer;public class Main15655_NM6 {public static int N, M;public static int[] arr, order;public static boolean[] vtd;public static StringBuilder sb;public static void solve(int idx, int cnt) {if(cnt == M) {for(int value: order) sb.append(arr[value]).append(" ");sb.append("\n");return;}for(int i = idx; i < N; i++) {if(!vtd[i]) {vtd[i] = true;order[cnt] = i;solve(i, cnt+1);vtd[i] = false;}}}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());M = Integer.parseInt(st.nextToken());arr = new int[N];order = new int[M];vtd = new boolean[N];sb = new StringBuilder();st = new StringTokenizer(bf.readLine());for(int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken());Arrays.sort(arr);solve(0, 0);System.out.println(sb.toString());}}cs 7. N과 M (7) :: 15656
12345678910111213141516171819202122232425262728293031323334353637383940414243import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;import java.util.StringTokenizer;public class Main15656_NM7 {public static int N, M;public static int[] arr, order;public static StringBuilder sb;public static void solve(int cnt) {if(cnt == M) {for(int val: order) sb.append(arr[val]).append(" ");sb.append("\n");return;}for(int i = 0; i < N; i++) {order[cnt] = i;solve(cnt+1);}}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());M = Integer.parseInt(st.nextToken());arr = new int[N];order = new int[M];sb = new StringBuilder();st = new StringTokenizer(bf.readLine());for(int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken());Arrays.sort(arr);solve( 0);System.out.println(sb.toString());}}cs 8. N과 M (8) :: 15657
1234567891011121314151617181920212223242526272829303132333435363738394041import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;import java.util.StringTokenizer;public class Main15657_NM8 {public static int N, M;public static int[] arr, order;public static StringBuilder sb;public static void solve(int idx, int cnt) {if(cnt == M) {for(int val: order) sb.append(arr[val]).append(" ");sb.append("\n");return;}for(int i = idx; i < N; i++) {order[cnt] = i;solve(i, cnt+1);}}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());M = Integer.parseInt(st.nextToken());arr = new int[N];order = new int[M];sb = new StringBuilder();st = new StringTokenizer(bf.readLine());for(int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken());Arrays.sort(arr);solve(0, 0);System.out.println(sb.toString());}}cs 9. N과 M (9) :: 15663
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.*;public class Main15663_NM9 {public static int N, M;public static int[] arr;public static boolean[] vtd;public static HashSet<String> set;public static List<String> list;public static void solve(int cnt, String str) {if(cnt == M) {if(!set.contains(str)) {set.add(str);list.add(str);}return;}for(int i = 0; i < N; i++) {if(!vtd[i]) {vtd[i] = true;solve(cnt+1, (str + (arr[i])+ " "));vtd[i] = false;}}}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());M = Integer.parseInt(st.nextToken());arr = new int[N];vtd = new boolean[N];list = new ArrayList<>();set = new HashSet<>();st = new StringTokenizer(bf.readLine());for(int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken());Arrays.sort(arr);solve( 0, "");StringBuilder sb = new StringBuilder();for (String s : list) {sb.append(s).append("\n");}System.out.println(sb.toString());}}cs 10. N과 M (10) :: 15664
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758import javax.imageio.IIOException;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.StringTokenizer;public class Main15664_NM10 {public static int N, M;public static int[] arr;public static boolean[] vtd;public static ArrayList<String> list;public static HashSet<String> set;public static void solve(int idx, int cnt, String str) {if(cnt == M) {if(!set.contains(str)) {set.add(str);list.add(str);}return;}for(int i = idx; i < N; i++) {if(!vtd[i]) {vtd[i] = true;solve(i, cnt+1, (str + arr[i] + " "));vtd[i] = false;}}}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());M = Integer.parseInt(st.nextToken());arr = new int[N];vtd = new boolean[N];list = new ArrayList<>();set = new HashSet<>();st = new StringTokenizer(bf.readLine());for(int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken());Arrays.sort(arr);solve(0, 0, "");StringBuilder sb = new StringBuilder();for(String val: list) sb.append(val).append("\n");System.out.println(sb.toString());}}cs 11. N과 M (11) :: 15665
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import 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.StringTokenizer;public class Main15665_NM11 {public static int N, M;public static int[] arr, order;public static StringBuilder sb;public static void solve(int cnt) {if(cnt == M) {for(Integer val: order) sb.append(arr[val]).append(" ");sb.append("\n");return;}for(int i = 0; i < arr.length; i++) {order[cnt] = i;solve(cnt+1);}}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());M = Integer.parseInt(st.nextToken());order = new int[M];HashSet<Integer> set = new HashSet<>();st = new StringTokenizer(bf.readLine());for(int i = 0; i < N; i++) {set.add(Integer.parseInt(st.nextToken()));}arr = new int[set.size()];int idx = 0;for(Integer val: set) {arr[idx] = val;idx++;}Arrays.sort(arr);sb = new StringBuilder();solve(0);System.out.println(sb.toString());}}cs 12. N과 M (12) :: 15666
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;import java.util.HashSet;import java.util.StringTokenizer;public class Main15666_NM12 {public static int N, M;public static int[] arr, order;public static StringBuilder sb;public static void solve(int idx, int cnt) {if(cnt == M) {for(int val: order) sb.append(arr[val]).append(" ");sb.append("\n");return;}for(int i = idx; i < arr.length; i++) {order[cnt] = i;solve(i, cnt+1);}}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(bf.readLine());N = Integer.parseInt(st.nextToken());M = Integer.parseInt(st.nextToken());HashSet<Integer> set = new HashSet<>();st = new StringTokenizer(bf.readLine());for(int i = 0; i < N; i++) set.add(Integer.parseInt(st.nextToken()));arr = new int[set.size()];order = new int[M];int idx = 0;for(Integer val:set) {arr[idx] = val;idx++;}Arrays.sort(arr);sb = new StringBuilder();solve(0, 0);System.out.println(sb.toString());}}cs #문제풀이
심심해서 풀어봤다. 딱 연습하기 좋은 문제들이다.
'ALGORITHM > BOJ' 카테고리의 다른 글
[백준] 13913 숨바꼭질 4 (0) 2021.07.09 [백준] 12851 숨바꼭질 2 (0) 2021.06.27 [백준] 20056 마법사 상어와 파이어볼 (0) 2021.04.06 [백준] 17779 게리맨더링2 (0) 2021.03.29 [백준] 2623 음악프로그램 (0) 2021.02.23