반응형
풀이 ) 완전 탐색
일곱 난쟁이의 키의 합이 100이므로 전체 9명의 합을 tot라고 했을 때
완전탐색을 통해서 2명의 합이 tot-100인 조합을 찾는 문제이다.
정렬은 sort()를 사용했다.
#include <cstdio> // scanf(), printf()
#include <algorithm> // sort()
using namespace std;
int main(void)
{
int arr[10] = {0, }, tot = 0;
for(int i = 0; i < 9; ++i){
scanf("%d", arr+i);
tot += arr[i];
}
sort(arr, arr+9);
int flag = 0;
for(int i = 0; i < 8; ++i){
for(int j = i; j < 9; ++j){
if (arr[i]+arr[j] == tot-100){
arr[i] = 0;
arr[j] = 0;
flag++;
break;
}
}
if(flag)
break;
}
for(int i= 0; i < 9; ++i)
if(arr[i])
printf("%d\n", arr[i]);
}
반응형
'문제풀이 > 백준' 카테고리의 다른 글
[C++] 백준 - 10973 : 이전 순열 (0) | 2020.03.12 |
---|---|
[C++] 백준 - 10972 : 다음 순열 (0) | 2020.03.12 |
[C++] 백준 - 9095 : 1, 2, 3 더하기 (0) | 2020.03.12 |
[C++] 백준 - 14500 : 테트로미노 (0) | 2020.03.11 |
[C++] 백준 - 1476 : 날짜 계산 (0) | 2020.03.10 |