Skip to content

Commit 7ad989b

Browse files
authored
Create 3.cpp
1 parent d3fcf11 commit 7ad989b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

20/3.cpp

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

0 commit comments

Comments
 (0)