[백준] 2309번 일곱 난쟁이 / C++

#문제

백준 2309번 일곱 난쟁이

#풀이

#include <algorithm>
#include <iostream>

using namespace std;

int arr[9];

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

	int total = 0;

	for (int i = 0; i < 9; ++i)
	{
		int x;
		cin >> x;
		arr[i] = x;
		total += x;
	}

	int target = total - 100;
	bool found = false;

	for (int i = 0; i < 8; ++i)
	{
		for (int j = 1; j < 9; ++j)
		{
			if (i == j)
			{
				continue;
			}

			if (arr[i] + arr[j] == target)
			{
				arr[i] = 0;
				arr[j] = 0;
				found = true;

				break;
			}
		}

		if (found == true)
		{
			break;
		}
	}

	sort(arr, arr + 9);


	for (int i = 0; i < 9; ++i)
	{
		if (arr[i] == 0)
		{
			continue;
		}

		cout << arr[i] << '\n';
	}

	return 0;
}

#정리

9명의 난쟁이 중에 키 합이 100이 되는 난쟁이 7명의 키를 오름차순으로 출력하는 문제다. 9명 난쟁이의 키를 모두 더한 뒤, 100을 빼고 남은 수가 합이 되는 난쟁이 2명을 찾았다. 그 2명을 뺀 나머지를 오름차순으로 출력하여 해결했다.




    Enjoy Reading This Article?

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

  • [백준] 1181번 단어 정렬 / C++
  • [백준] 9613번 GCD 합 / C++
  • [백준] 2003번 수들의 합 2 / C++
  • [백준] 1940번 주몽 / C++
  • [백준] 1244번 스위치 켜고 끄기 / C++