[백준] 2003: 수들의 합2
2021. 2. 2. 15:15ㆍAlgorithm/백준
2003번: 수들의 합 2
첫째 줄에 N(1 ≤ N ≤ 10,000), M(1 ≤ M ≤ 300,000,000)이 주어진다. 다음 줄에는 A[1], A[2], …, A[N]이 공백으로 분리되어 주어진다. 각각의 A[x]는 30,000을 넘지 않는 자연수이다.
www.acmicpc.net
#include<iostream>
#include<vector>
using namespace std;
int n, m;
vector<int>arr;
int solution() {
int sum = 0, pos = 0;
int start = 0, end = 0;
for (start = 0;start < arr.size();start++) {
while (sum < m&&end < arr.size()) {
sum += arr[end];
end++;
}
if (sum == m) pos++;
sum -= arr[start];
}
return pos;
}
int main() {
cin >> n >> m;
for (int i = 0,t=0;i < n;i++) {
cin >> t;
arr.push_back(t);
}
cout<<solution();
}
'Algorithm > 백준' 카테고리의 다른 글
[백준] 2206번: 벽 부수고 이동하기 (0) | 2021.02.05 |
---|---|
[백준] 1261번: 알고스팟 (0) | 2021.02.05 |
[백준] 1182번: 부분수열의 합 (0) | 2021.02.02 |
[백준] 15666번: N과 M(12) (0) | 2021.02.01 |
[백준] 15665번: N과 M(11) (0) | 2021.02.01 |