Skip to content

Commit 9804780

Browse files
authored
Merge pull request #1807 from AlgorithmWithGod/Ukj0ng
[20260121] BOJ / G4 / 수들의 합 4 / 한종욱
2 parents a461503 + 38c1c60 commit 9804780

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
```
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
private static Map<Integer, Integer> map;
9+
private static int[] arr;
10+
private static int N, K;
11+
private static long answer;
12+
13+
public static void main(String[] args) throws IOException {
14+
init();
15+
16+
bw.write(answer + "\n");
17+
bw.flush();
18+
bw.close();
19+
br.close();
20+
}
21+
22+
private static void init() throws IOException {
23+
StringTokenizer st = new StringTokenizer(br.readLine());
24+
N = Integer.parseInt(st.nextToken());
25+
K = Integer.parseInt(st.nextToken());
26+
27+
arr = new int[N];
28+
map = new HashMap<>();
29+
30+
st = new StringTokenizer(br.readLine());
31+
for (int i = 0; i < N; i++) {
32+
arr[i] = Integer.parseInt(st.nextToken());
33+
}
34+
35+
int currentSum = 0;
36+
37+
for (int n : arr) {
38+
currentSum += n;
39+
40+
if (currentSum == K) answer++;
41+
42+
if (map.containsKey(currentSum-K)) {
43+
answer += map.get(currentSum-K);
44+
}
45+
46+
map.put(currentSum, map.getOrDefault(currentSum, 0) + 1);
47+
}
48+
}
49+
}
50+
```

0 commit comments

Comments
 (0)