[백준] 2003번 수들의 합 2 / C++

#문제

백준 2003번 수들의 합 2

#풀이

#include <iostream>

using namespace std;

int arr[10000];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n, m;
	int cnt = 0;
	cin >> n >> m;

	for (int i = 0; i < n; ++i)
	{
		int x;
		cin >> x;

		arr[i] = x;
	}

	for (int i = 0; i < n; ++i)
	{
		int total = 0;
		int j = i;

		while (total < m && j < n)
		{
			total += arr[j++];

			if (total == m)
			{
				cnt++;
			}
		}
	}

	cout << cnt;

	return 0;
}

#정리

n개의 수를 받는다. 받은 수에서 연속된 수의 합이 m과 같다면 카운팅을 하고, m을 넘어간다면 패스하고 다음 숫자로 넘어간다. 마지막 숫자까지 배열의 합을 진행하고, 카운팅을 출력하는 프로그램을 만드는 문제. 간단한 문제다. 수열의 합이 m을 넘어가는 지와 배열 끝이 넘어가는 지 신경쓰며 코드를 작성하여 해결.




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • [백준] 9613번 GCD 합 / C++
  • [백준] 2309번 일곱 난쟁이 / C++
  • [백준] 1018번 체스판 다시 칠하기 / C++
  • [백준] 1244번 스위치 켜고 끄기 / C++
  • [백준] 2563번 색종이 / C++