[백준] 2309번 일곱 난쟁이 / C++
#문제
#풀이
#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: