[백준] 1940번 주몽 / C++

#문제

백준 1940번 주몽

#풀이

#include <iostream>
#include <vector>

using namespace std;

vector<int> arr;

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

	int n, m;
	cin >> n >> m;

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

		arr.push_back(x);
	}

	int ans = 0;

	for (int i = 0; i < n - 1; ++i)
	{
		for (int j = i + 1; j < n; ++j)
		{
			if (arr[i] + arr[j] == m)
			{
				ans++;
			}
		}
	}

	cout << ans;

	return 0;
}

#정리

n개의 값을 받고, 2개 원소의 값이 m과 같을 경우의 갯수를 구하는 문제. 이중포문을 사용해서 해결.




    Enjoy Reading This Article?

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

  • [백준] 9613번 GCD 합 / C++
  • [백준] 1934번 최소공배수 / C++
  • [백준] 2309번 일곱 난쟁이 / C++
  • [백준] 10773번 제로 / C++
  • [백준] 1010번 다리 놓기 / C++