Skip to content

Commit df01d14

Browse files
authored
Create 3.java
1 parent 7ad989b commit df01d14

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

20/3.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
3+
class Main {
4+
public static int n = 5; // 데이터의 개수 N
5+
public static int m = 5; // 찾고자 하는 부분합 M
6+
public static int[] arr = {1, 2, 3, 2, 5}; // 전체 수열
7+
8+
public static void main(String[] args) {
9+
int cnt = 0;
10+
int intervalSum = 0;
11+
int end = 0;
12+
13+
// start를 차례대로 증가시키며 반복
14+
for (int start = 0; start < n; start++) {
15+
// end를 가능한 만큼 이동시키기
16+
while (intervalSum < m && end < n) {
17+
intervalSum += arr[end];
18+
end += 1;
19+
}
20+
// 부분합이 m일 때 카운트 증가
21+
if (intervalSum == m) {
22+
cnt += 1;
23+
}
24+
intervalSum -= arr[start];
25+
}
26+
27+
System.out.println(cnt);
28+
}
29+
}

0 commit comments

Comments
 (0)