Skip to content

Commit 6d6c7f3

Browse files
authored
수 이어쓰기 (#77)
1 parent fd12e08 commit 6d6c7f3

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

pr/수 이어쓰기/Main.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
static int n;
6+
static int max = Integer.MIN_VALUE;
7+
static List<Integer> list;
8+
static List<Integer> maxList;
9+
10+
public static void main(String[] args) {
11+
12+
Scanner sc = new Scanner(System.in);
13+
14+
n = sc.nextInt();
15+
16+
solve();
17+
18+
}
19+
20+
public static void solve() {
21+
first(n);
22+
23+
System.out.println(max);
24+
25+
for (int num : maxList) {
26+
System.out.print(num + " ");
27+
}
28+
}
29+
30+
public static void first(int n) {
31+
// 두번째 수 구하기
32+
for (int i = 1; i <= n; i++) {
33+
list = new ArrayList<>();
34+
list.add(n);
35+
list.add(i);
36+
dfs(n, i);
37+
}
38+
39+
}
40+
41+
public static void dfs(int before, int current) {
42+
43+
int next = before - current;
44+
45+
if (next < 0) {
46+
if (max < list.size()) {
47+
max = list.size();
48+
maxList = new ArrayList<>(list);
49+
}
50+
return;
51+
}
52+
53+
// 세번째 수 ~ ?번째수까지 구하기
54+
list.add(next);
55+
dfs(current, next);
56+
57+
}
58+
59+
}

0 commit comments

Comments
 (0)