728x90

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
int num[9], result[7]; | |
int main(void) { | |
ios::sync_with_stdio(false), cin.tie(nullptr); | |
for (int i = 0; i < 9; i++) cin >> num[i]; | |
for (int a = 0; a < 8; a++) { // all combination | |
int total = 0; | |
for (int b = a + 1; b < 9; b++) { | |
total = 0; | |
for (int c = 0, i = 0; c < 9; c++) { | |
if (c != a && c != b) result[i++] = num[c]; // key point | |
} | |
for (int i = 0; i < 7; i++) total += result[i]; | |
if (total == 100) break; | |
} | |
if (total == 100) break; | |
} | |
sort(result, result + 7); | |
for (int i = 0; i < 7; i++) cout << result[i] << '\n'; | |
} | |
// #include <bits/stdc++.h> | |
// #define N 9 | |
// using namespace std; | |
// int main() { | |
// ios::sync_with_stdio(0), cin.tie(0); | |
// int a[N] = {}, sum = 0; | |
// for (int i = 0; i < N; i++) cin >> a[i], sum += a[i]; | |
// sort(a, a + N); | |
// for (int i = 0; i < N; i++) { | |
// for (int j = i + 1; j < N; j++) { | |
// if (sum - a[i] - a[j] == 100) { | |
// for (int k = 0; k < 9; k++) | |
// if (k != i && k != j) cout << a[k] << '\n'; | |
// return 0; | |
// } | |
// } | |
// } | |
// } | |
/* | |
일곱 난쟁이(2309) | |
O(N^3) | |
값을 입력 받은 후 바로 정렬하고 시작하는게 좋다 sort() | |
9명 전체 합을 구한 후 나머지 2명을 빼, 그 값이 100인지 확인하면 된다 | |
모든 경우의 수를 고려하려 반복하면 된다(브루트 포스) | |
단, 해당 결과를 찾았다면 return 해주도록 하자 | |
*/ |
'Algorithm > C&C++' 카테고리의 다른 글
백준 1267번: 핸드폰 요금(c/c++, c/cpp) (0) | 2021.10.27 |
---|---|
백준 10093번: 숫자(c/c++, c/cpp) (0) | 2021.10.26 |
백준 2587번: 대표값2(c/c++, c/cpp) (0) | 2021.10.06 |
백준 2576번: 홀수(c/c++, c/cpp) (0) | 2021.10.05 |
백준 2562번: 최댓값(c/c++, c/cpp) (0) | 2021.10.05 |