알고리즘/백준

백준 2309번 일곱난쟁이

가을맛 2020. 10. 11. 18:15

처음에 vector 헤더써서 풀었는데 런타임에러의 지옥에 빠졌다. 도저히 원인을 모르겠어서 그냥 배열로 하니 바로 정답이다. 대체 뭐가 문제였던거지.

#include<iostream>
//#include<vector>
#include<algorithm>


using namespace std;
int nine_dwarfs[9];
int result[7];
int sum = 0;

bool compare(int a, int b) {
	return a > b;
}


int find(const int n, const int candidate) {

	//sum이 100을 넘거나 7명 이상의 난쟁이 선택 했거나 후보가 남아있지 않을 시 아무것도 하지 않고 종료
	if (sum > 100 || n == 7 || candidate == 9)
		return sum;

	else if (sum == 100)
		return sum;


	for (int c = candidate; c < 9; c++)
	{
		result[n] = nine_dwarfs[c];
		sum += nine_dwarfs[c];
		if (find(n + 1, c + 1) == 100)
			return sum;
		result[n] = 0;
		sum -= nine_dwarfs[c];
	}

	return sum;


}

int main() {


	//입력
	for (int i = 0; i < 9; i++) {
		cin >> nine_dwarfs[i];
	}

	//시작하기전에 오름차순으로 정렬
	sort(nine_dwarfs, nine_dwarfs + 9);

	find(0, 0);

	for (int i = 0; i < 7; i++) {
		cout << result[i] << endl;
	}

	return 0;

}