C++/코드트리

[코드트리] A, B, C, D 찾기 2

sondiaa 2023. 8. 25. 00:33

 

https://www.codetree.ai/missions/5/problems/find-a-b-c-d-2?&utm_source=clipboard&utm_medium=text 

a, b, c, d 값을 정해놓고 완전탐색 하는 방식으로 풀었다.

처음에 어떻게 풀어보려고 노력했는데,, 안돼서 완전탐색으로 접근했다.

#include <iostream>
#include <algorithm>
using namespace std;
int map[15];
int main() {
    for(int i=0; i<15; i++)
        cin >> map[i];
    sort(map, map+15);
    for(int a=1; a<40; a++){
        for(int b=a; b<=40; b++){
            for(int c=b; c<=40; c++){
                for(int d = c; d<=40; d++){
                    int temp[15] = {a, b, c, d, a + b, a + c, a + d, b + c, b + d, c + d, a + b + c, a+ b + d, a + c + d, b + c + d, a + b + c + d};
                    sort(temp, temp + 15);
                    int flag = 0;
                    for(int i=0; i<15; i++){
                        if(map[i] != temp[i])
                            flag = 1;
                    }
                    if(flag == 0){
                        cout << a << ' ' << b << ' ' << c << ' ' << d;
                        return 0;
                    }
                }
            }
        }
    }
    return 0;
}