문제 : https://www.acmicpc.net/problem/2003
[알고리즘풀이]
배열의 원소의 값이 변하지 않으므로, 다이나믹 프로그래밍을 이용해 문제를 풀 수 있다.
( 자세한 설명 : https://travelbeeee.tistory.com/115 )
#include<iostream>
using namespace std;
int N[10000];
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> N[i];
if (i != 0)
N[i] += N[i - 1];
}
int ans = 0;
for(int i = 0; i < n; i++)
for (int j = i; j < n; j++) {
if (i == 0) {
if (N[j] == m)
ans++;
}
else{
if (N[j] - N[i - 1] == m)
ans++;
}
}
cout << ans;
}
'Problem Solving > BOJ' 카테고리의 다른 글
[BOJ] 2225 - 합분해 (0) | 2019.10.01 |
---|---|
[BOJ] 1914 - 하노이 탑 (0) | 2019.10.01 |
[BOJ] 10819 - 차이를 최대로 (0) | 2019.09.30 |
[BOJ] 1520 - 내리막 길 (0) | 2019.09.29 |
[BOJ] 2522 - '별 찍기 - 12' (0) | 2019.09.29 |