Skip to content

Commit 020dedf

Browse files
committed
[Permutation] backjoon-N과M Series
1 parent 794e389 commit 020dedf

File tree

15 files changed

+625
-1
lines changed

15 files changed

+625
-1
lines changed

Diff for: README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,21 @@
7878

7979
| # || Problem | Note |
8080
| :-: | :----: | :------------------------------------------------------------------------------------------------------------------ | :------------------------ |
81-
| 01 | | [Baekjoon-13251 조약돌 꺼내기](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Probability/P13251) | |
81+
| 01 | | [Baekjoon-13251 조약돌 꺼내기](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Probability/P13251) | |
82+
83+
### Series : N과 M (1-12)
84+
85+
| # || Problem | Note |
86+
| :-: | :----: | :------------------------------------------------------------------------------------------------------------------ | :------------------------ |
87+
| 01 | | [Baekjoon-15649 N과 M (1)](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Series/NandM/P15649) | |
88+
| 02 | | [Baekjoon-15650 N과 M (2)](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Series/NandM/P15650) | |
89+
| 03 | | [Baekjoon-15651 N과 M (3)](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Series/NandM/P15651) | 시간초과이슈 : BufferedWriter |
90+
| 04 | | [Baekjoon-15652 N과 M (4)](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Series/NandM/P15652) | |
91+
| 05 | | [Baekjoon-15654 N과 M (5)](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Series/NandM/P15654) | |
92+
| 06 | | [Baekjoon-15655 N과 M (6)](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Series/NandM/P15655) | |
93+
| 07 | | [Baekjoon-15656 N과 M (7)](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Series/NandM/P15656) | |
94+
| 08 | | [Baekjoon-15657 N과 M (8)](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Series/NandM/P15657) | |
95+
| 09 | | [Baekjoon-15663 N과 M (9)](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Series/NandM/P15663) | |
96+
| 10 | | [Baekjoon-15664 N과 M (10)](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Series/NandM/P15664) | |
97+
| 11 | | [Baekjoon-15665 N과 M (11)](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Series/NandM/P15665) | |
98+
| 12 | | [Baekjoon-15666 N과 M (12)](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Series/NandM/P15666) | |

Diff for: src/Series/NandM/P15649/Main.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package Series.NandM.P15649;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileInputStream;
5+
import java.io.InputStreamReader;
6+
import java.util.Arrays;
7+
import java.util.StringTokenizer;
8+
9+
public class Main {
10+
11+
static int N, M;
12+
static int[] nums = new int[8];
13+
static boolean[] visited = new boolean[8];
14+
15+
public static void main(String[] args) throws Exception {
16+
// System.setIn(new FileInputStream("src/Series/NandM/input.txt"));
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
StringTokenizer st = new StringTokenizer(br.readLine());
19+
20+
N = Integer.parseInt(st.nextToken());
21+
M = Integer.parseInt(st.nextToken());
22+
23+
dfs(0);
24+
}
25+
26+
static void dfs(int count){
27+
if (count == M) {
28+
for (int i = 0; i < M; i++) {
29+
System.out.print(nums[i] + " ");
30+
}
31+
System.out.println("");
32+
return;
33+
}
34+
for (int i = 0; i < N; i++) {
35+
if (!visited[i]) {
36+
visited[i] = true;
37+
nums[count] = i + 1;
38+
dfs(count + 1);
39+
visited[i] = false;
40+
}
41+
}
42+
}
43+
}

Diff for: src/Series/NandM/P15650/Main.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Series.NandM.P15650;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileInputStream;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class Main {
9+
10+
static int N, M;
11+
static int[] nums = new int[8];
12+
static boolean[] visited = new boolean[8];
13+
14+
public static void main(String[] args) throws Exception {
15+
// System.setIn(new FileInputStream("src/Series/NandM/input.txt"));
16+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
19+
N = Integer.parseInt(st.nextToken());
20+
M = Integer.parseInt(st.nextToken());
21+
22+
dfs(0, 0);
23+
}
24+
25+
static void dfs(int count, int last){
26+
if (count == M) {
27+
for (int i = 0; i < M; i++) {
28+
System.out.print(nums[i] + " ");
29+
}
30+
System.out.println("");
31+
return;
32+
}
33+
for (int i = last; i < N; i++) {
34+
if (!visited[i]) {
35+
visited[i] = true;
36+
nums[count] = i + 1;
37+
dfs(count + 1, i + 1);
38+
visited[i] = false;
39+
}
40+
}
41+
}
42+
}

Diff for: src/Series/NandM/P15651/Main.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package Series.NandM.P15651;
2+
3+
import java.io.*;
4+
import java.util.Arrays;
5+
import java.util.StringTokenizer;
6+
7+
public class Main {
8+
9+
static int N, M;
10+
static int[] nums = new int[7];
11+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
12+
13+
public static void main(String[] args) throws Exception {
14+
System.setIn(new FileInputStream("src/Series/NandM/input.txt"));
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
18+
N = Integer.parseInt(st.nextToken());
19+
M = Integer.parseInt(st.nextToken());
20+
21+
dfs(0);
22+
23+
br.close();
24+
bw.flush();
25+
bw.close();
26+
}
27+
28+
static void dfs(int count) throws IOException {
29+
if (count == M) {
30+
for (int i = 0; i < M; i++) {
31+
bw.write(nums[i] + " ");
32+
}
33+
bw.newLine();
34+
return;
35+
}
36+
for (int i = 0; i < N; i++) {
37+
nums[count] = i + 1;
38+
dfs(count + 1);
39+
}
40+
}
41+
}

Diff for: src/Series/NandM/P15651/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## 시간초과 이슈
2+
3+
처음에 코드를 제출했을 때 시간 초과 이슈가 있었다. 이 부분에 대하여 찾아보던 중, C++ 코드들은 똑같은 로직으로 잘 돌아가는데 내 코드는 안 돌아가는 것이 이상하다는 것을 알게 되었다. 그래서 찾아보니 자바의 경우 Scanner, System.out.println()를 이용해 입출력을 구현할 시에 속도가 많이 느리다고 한다.
4+
따라서 **BufferedReader, BufferedWriter**를 사용해야 한다고 한다. 실제로 이 문제에 관하여 max 값인 '7 7'을 입력으로 넣고 돌렸을 때, BufferedWriter를 사용했을 경우 출력 속도가 훨씬 빠른 것을 확인할 수 있었다.
5+
6+
### BufferedReader, BufferedWriter
7+
8+
#### close() 메서드
9+
10+
- 입력소스를 닫음으로써 사용하고 있던 자원을 반환한다.
11+
- 프로그램이 종료될 때, 사용하고 닫지 않은 스트림을 JVM이 자동적으로 닫아주기는 하지만, 스트림을 사용해 모든 작업을 마치고 난 후에는 close를 호출해 반드시 닫아주어야 한다고 한다.
12+
- `System.in`, `System.out` 과 같은 표준 입출력 스트림은 닫아주지 않아도 된다.
13+
14+
#### flush() 메서드
15+
16+
스트림의 버퍼에 있는 모든 내용을 출력 소스에 쓴다. flush()를 해주지 않을 경우 BufferedWriter를 사용했을 때 그 내용이 정확하게 출력되지 않는다.

Diff for: src/Series/NandM/P15652/Main.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package Series.NandM.P15652;
2+
3+
import java.io.*;
4+
import java.util.StringTokenizer;
5+
6+
public class Main {
7+
8+
static int N, M;
9+
static int[] nums = new int[8];
10+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
12+
public static void main(String[] args) throws Exception {
13+
// System.setIn(new FileInputStream("src/Series/NandM/input.txt"));
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
17+
N = Integer.parseInt(st.nextToken());
18+
M = Integer.parseInt(st.nextToken());
19+
20+
dfs(0, 0);
21+
22+
br.close();
23+
bw.flush();
24+
bw.close();
25+
}
26+
27+
static void dfs(int count, int last) throws IOException {
28+
if (count == M) {
29+
for (int i = 0; i < M; i++) {
30+
bw.write(nums[i] + " ");
31+
}
32+
bw.newLine();
33+
return;
34+
}
35+
for (int i = last; i < N; i++) {
36+
nums[count] = i + 1;
37+
dfs(count + 1, i);
38+
}
39+
}
40+
}

Diff for: src/Series/NandM/P15654/Main.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package Series.NandM.P15654;
2+
3+
import java.io.*;
4+
import java.util.Arrays;
5+
import java.util.StringTokenizer;
6+
7+
public class Main {
8+
9+
static int N, M;
10+
static int[] nums;
11+
static int[] ans = new int[8];
12+
static boolean[] visited = new boolean[8];
13+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
14+
15+
public static void main(String[] args) throws Exception {
16+
// System.setIn(new FileInputStream("src/Series/NandM/input.txt"));
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
StringTokenizer st = new StringTokenizer(br.readLine());
19+
20+
N = Integer.parseInt(st.nextToken());
21+
M = Integer.parseInt(st.nextToken());
22+
23+
nums = new int[N];
24+
st = new StringTokenizer(br.readLine());
25+
for (int i = 0; i < N; i++) {
26+
nums[i] = Integer.parseInt(st.nextToken());
27+
}
28+
29+
Arrays.sort(nums);
30+
dfs(0);
31+
32+
br.close();
33+
bw.flush();
34+
bw.close();
35+
}
36+
37+
static void dfs(int count) throws IOException {
38+
if (count == M) {
39+
for (int i = 0; i < M; i++) {
40+
bw.write(ans[i] + " ");
41+
}
42+
bw.newLine();
43+
return;
44+
}
45+
for (int i = 0; i < N; i++) {
46+
if (!visited[i]) {
47+
visited[i] = true;
48+
ans[count] = nums[i];
49+
dfs(count + 1);
50+
visited[i] = false;
51+
}
52+
}
53+
}
54+
}

Diff for: src/Series/NandM/P15655/Main.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package Series.NandM.P15655;
2+
3+
import java.io.*;
4+
import java.util.Arrays;
5+
import java.util.StringTokenizer;
6+
7+
public class Main {
8+
9+
static int N, M;
10+
static int[] nums;
11+
static int[] ans = new int[8];
12+
static boolean[] visited = new boolean[8];
13+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
14+
15+
public static void main(String[] args) throws Exception {
16+
// System.setIn(new FileInputStream("src/Series/NandM/input.txt"));
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
StringTokenizer st = new StringTokenizer(br.readLine());
19+
20+
N = Integer.parseInt(st.nextToken());
21+
M = Integer.parseInt(st.nextToken());
22+
23+
nums = new int[N];
24+
st = new StringTokenizer(br.readLine());
25+
for (int i = 0; i < N; i++) {
26+
nums[i] = Integer.parseInt(st.nextToken());
27+
}
28+
29+
Arrays.sort(nums);
30+
dfs(0, 0);
31+
32+
br.close();
33+
bw.flush();
34+
bw.close();
35+
}
36+
37+
static void dfs(int count, int last) throws IOException {
38+
if (count == M) {
39+
for (int i = 0; i < M; i++) {
40+
bw.write(ans[i] + " ");
41+
}
42+
bw.newLine();
43+
return;
44+
}
45+
for (int i = last; i < N; i++) {
46+
if (!visited[i]) {
47+
visited[i] = true;
48+
ans[count] = nums[i];
49+
dfs(count + 1, i + 1);
50+
visited[i] = false;
51+
}
52+
}
53+
}
54+
}

Diff for: src/Series/NandM/P15656/Main.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package Series.NandM.P15656;
2+
3+
import java.io.*;
4+
import java.util.Arrays;
5+
import java.util.StringTokenizer;
6+
7+
public class Main {
8+
9+
static int N, M;
10+
static int[] nums;
11+
static int[] ans = new int[7];
12+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
14+
public static void main(String[] args) throws Exception{
15+
// System.setIn(new FileInputStream("src/Series/NandM/input.txt"));
16+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
19+
N = Integer.parseInt(st.nextToken());
20+
M = Integer.parseInt(st.nextToken());
21+
22+
nums = new int[N];
23+
st = new StringTokenizer(br.readLine());
24+
for (int i = 0; i < N; i++) {
25+
nums[i] = Integer.parseInt(st.nextToken());
26+
}
27+
28+
Arrays.sort(nums);
29+
dfs(0);
30+
31+
br.close();
32+
bw.flush();
33+
bw.close();
34+
}
35+
36+
static void dfs(int count) throws IOException {
37+
if (count == M) {
38+
for (int i = 0; i < M; i++) {
39+
bw.write(ans[i] + " ");
40+
}
41+
bw.newLine();
42+
return;
43+
}
44+
for (int i = 0; i < N; i++) {
45+
ans[count] = nums[i];
46+
dfs(count + 1);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)