본문 바로가기
카테고리 없음

[JAVA]백준_2003_수들의 합2

by 박 현 황 2021. 2. 7.

문제링크

 

www.acmicpc.net/problem/2003

 

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

 

 

 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main_2003 {

	static int N;
	static int M;
	static int arr[];
	static boolean isSelected[];
	static int sum,result;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		N = Integer.parseInt(st.nextToken());
		M = Integer.parseInt(st.nextToken());
		arr = new int[N];
		isSelected = new boolean[N];
		st = new StringTokenizer(br.readLine());
		for(int i=0;i<N;i++)
			arr[i]  = Integer.parseInt(st.nextToken());
		for(int i=0;i<N;i++) {
			sum = 0;
			for(int j=i;j<N;j++) {
				sum += arr[j];
				if(sum == M) {
					result ++;
					continue;
				}
				if(sum>M)
					continue;
			}
		}
		System.out.println(result);
	}
}