BOJ 문제풀이

백준 2003번 c++ 풀이

koreasunoo 2021. 10. 10. 23:38

전략:

1. 투포인터 알고리즘을 사용하여 푼다.

코드:

#include <bits/stdc++.h>
using namespace std;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int N, M;
	cin>>N>>M;
	int arr[N];
	for(int i= 0; i<N; ++i){
		cin>>arr[i];
	}
	int l =0, r= 0, sum= 0, result = 0;
	while(l<=r){
		if(sum==M){
			result++;
		}
		if(sum>=M){
			sum-=arr[l];
			l++;
		}
		else if(r>=N){
			break;
		}
		else{
			sum+= arr[r];
			r++;
		}
	}
	cout<<result<<"\n";
}

'BOJ 문제풀이' 카테고리의 다른 글

백준 23175번 c++ 풀이  (0) 2021.10.11
백준 1644번 c++ 풀이  (0) 2021.10.10
백준 11728번 c++ 풀이  (0) 2021.10.10
백준 4673번 c++ 풀이  (0) 2021.10.10
백준 2193번 c++ 풀이  (0) 2021.10.10