백준 2003번

2022. 2. 21. 03:22·Study/Algorithm

https://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

#include <iostream>
#include "vector"

using namespace std;

vector<int> board;

int main() {
	int N, M;
	cin >> N >> M;

	for (int i = 0; i < N; i++) {
		int input;
		cin >> input;
		board.push_back(input);
	}
	board.push_back(0);

	int answer = 0;

	int start = 0;
	int end = 0;
	int sum = 0;

	while (end <= N) {
		if (sum >= M) {
			sum -= board[start];
			start++;
		}
		else if (sum < M) {
			sum += board[end];
			end++;
		}
		
		if (sum == M)
			answer++;
	}

	cout << answer;
}

'Study > Algorithm' 카테고리의 다른 글

백준 10025번  (0) 2022.02.28
백준 1940번  (0) 2022.02.28
백준 21921번  (0) 2022.02.21
백준 20922번  (0) 2022.02.21
백준 15489번  (0) 2022.02.14
'Study/Algorithm' 카테고리의 다른 글
  • 백준 10025번
  • 백준 1940번
  • 백준 21921번
  • 백준 20922번
_WooHyun_
_WooHyun_
  • _WooHyun_
    Nerd
    _WooHyun_
  • 전체
    오늘
    어제
    • 분류 전체보기 (79)
      • Study (60)
        • Algorithm (24)
        • Unreal Engine (19)
        • C++ (1)
        • Maya (1)
        • GoLang (3)
        • Mysql (3)
        • Linux (7)
        • Server (2)
      • Projects (0)
        • Unreal Engine (0)
        • Server (0)
      • 개발일지 (8)
        • Unreal Engine (7)
        • Art (1)
        • Server (0)
      • 미래 (5)
      • 개발아이디어 (0)
      • 잡지식 (2)
  • 블로그 메뉴

    • 홈
    • 방명록
    • 글쓰기
    • 블로그설정
  • 링크

    • GitHub
  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
_WooHyun_
백준 2003번
상단으로

티스토리툴바