Skip to content

Commit dff4d18

Browse files
committed
[20251208] BOJ / G4 / 게임 / 김민진
1 parent 9632be8 commit dff4d18

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

zinnnn37/202512/8 BOJ G4 게임.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
```java
2+
import java.io.*;
3+
import java.util.Arrays;
4+
import java.util.StringTokenizer;
5+
6+
public class BJ_28423_게임 {
7+
8+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
private static StringTokenizer st;
11+
12+
private static int L, R, ans;
13+
private static int[] memo;
14+
private static boolean[] checked;
15+
16+
public static void main(String[] args) throws IOException {
17+
init();
18+
sol();
19+
}
20+
21+
private static void init() throws IOException {
22+
st = new StringTokenizer(br.readLine());
23+
L = Integer.parseInt(st.nextToken());
24+
R = Integer.parseInt(st.nextToken());
25+
26+
ans = 0;
27+
memo = new int[100_001];
28+
checked = new boolean[100_001];
29+
Arrays.fill(memo, 2);
30+
}
31+
32+
private static void sol() throws IOException {
33+
for (int i = L; i <= R; i++) {
34+
ans += dfs(i);
35+
}
36+
bw.write(ans + "");
37+
bw.flush();
38+
bw.close();
39+
br.close();
40+
}
41+
42+
private static int dfs(int cur) {
43+
if (memo[cur] != 2) {
44+
return memo[cur];
45+
}
46+
47+
if (checked[cur]) {
48+
return memo[cur] = 0;
49+
}
50+
51+
checked[cur] = true;
52+
53+
int A = 0;
54+
int B = 1;
55+
56+
int tmp = cur;
57+
while (tmp != 0) {
58+
A += tmp % 10;
59+
B *= tmp % 10;
60+
tmp /= 10;
61+
}
62+
63+
int res = Integer.parseInt(String.valueOf(A) + String.valueOf(B));
64+
65+
if (cur == res) {
66+
memo[cur] = 1;
67+
} else if (res > 100000) {
68+
memo[cur] = -1;
69+
} else {
70+
memo[cur] = dfs(res);
71+
}
72+
checked[cur] = false;
73+
74+
return memo[cur];
75+
}
76+
77+
}
78+
```

0 commit comments

Comments
 (0)