[PS 백준 - 2.9] 10448번: 유레카 이론

PongkiJoa·2021년 6월 30일
0

PS Diary - 백준

목록 보기
22/54
post-thumbnail

문제 정보

백준 10448번 - 바로가기

  • 난이도: 브론즈 2
  • 알고리즘: 브루트포스 알고리즘

코멘트

1000 이하의 삼각수를 모두 구하고, 삼중 반복문으로 세 수를 선택해 더해서 nC3_{n}\mathrm{C}_{3} 가지 경우 모두를 탐색했다.


소스 코드

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main() {
    ios::sync_with_stdio(false); 
    cin.tie(0); 
    cout.tie(0);
    
    int n, x;
    cin >> n;
    vector<int> tri;
    int cc = 1;
    while (cc * (cc + 1) / 2 <= 1000) {
        tri.push_back(cc * (cc + 1) / 2);
        cc++;
    }

    for (int i = 0; i < n; i++) {
        cin >> x;

        int r = 0, flag=0;
        for (int a = 0; a < tri.size(); a++) {
            for (int b = 0; b < tri.size(); b++) {
                for (int c = 0; c < tri.size(); c++) {
                    r = tri[a] + tri[b] + tri[c];
                    if (r == x) {
                        flag = 1;
                        break;
                    }
                }
                if (r == x) break;
            }
            if (r == x) break;

        }
        if (flag) cout << 1  << "\n";
        else cout << 0 << "\n";


    }
}
profile
컴공 20학번

0개의 댓글

관련 채용 정보