728x90
문제
https://www.acmicpc.net/problem/2003
📌 문제풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String firtLine = br.readLine();
StringTokenizer st = new StringTokenizer(firtLine, " ");
// 수열 개수
int N = Integer.parseInt(st.nextToken());
int[] arr = new int[N];
// 합
int M = Integer.parseInt(st.nextToken());
String secondLine = br.readLine();
st = new StringTokenizer(secondLine, " ");
for(int i=0; i<N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
int start = 0;
int end = 0;
int sum = 0;
int count = 0;
// 투 포인터 알고리즘
while(true) {
if(M==sum) {
count++;
}
if(M<=sum) {
sum-=arr[start++];
continue;
}
if(end==N) {
break;
}
if(sum<M) {
sum+= arr[end++];
}
}
System.out.println(count);
}
}
728x90
'[알고리즘] > 알고리즘' 카테고리의 다른 글
[알고리즘] 백준 10859번 : 뒤집어진 소수 (0) | 2022.04.14 |
---|---|
[알고리즘] 백준 1644번 : 소수의 연속합 (0) | 2022.04.14 |
[알고리즘] 백준 8896번 : 가위 바위 보 (실버3) (0) | 2022.04.13 |
백준 10820번 : 문자열 분석 (브론즈2) (0) | 2022.04.12 |
백준 1718번 : 암호 (브론즈2) (0) | 2022.04.12 |