알고리즘/백준

[백준/c++/swift] 10448번: 유레카 이론

녕이 2022. 11. 3. 14:06
728x90

 

https://www.acmicpc.net/problem/10448

 

10448번: 유레카 이론

프로그램은 표준입력을 사용한다. 테스트케이스의 개수는 입력의 첫 번째 줄에 주어진다. 각 테스트케이스는 한 줄에 자연수 K (3 ≤ K ≤ 1,000)가 하나씩 포함되어있는 T개의 라인으로 구성되어

www.acmicpc.net

 

 

완전 탐색 문제로, n의 범위가 1000 밖에 되지 않으므로 3중 for문을 사용해서 해결했다!

 

 

c++

#include <iostream>
using namespace std;
int eureka[1001];

bool check(int n){
    for(int i=1; i<n; i++){
        for(int j=1; j<n; j++){
            for(int k=1; k<n; k++){
                if(eureka[i] + eureka[j] + eureka[k] == n) {
                    return true;
                }
            }
        }
    }
    return false;
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    for(int i=1; i<=1000; i++) eureka[i] = i*(i+1)/2;
    while(t--){
        int n;
        cin >> n;
        cout << check(n) << '\n';
    }
    return 0;
}

 

Swift

import Foundation

var test = Int(readLine()!)!
var T = Array(repeating: 0, count: 1001)
for i in 1...1000 {
    T[i] = i * (i + 1) / 2
}

func check(n: Int) -> Bool {
    for i in 1..<n {
        for j in 1..<n {
            for k in 1..<n {
                if T[i] + T[j] + T[k] == n {
                    return true
                }
            }
        }
    }
    return false
}

while test > 0 {
    let n = Int(readLine()!)!
    print(check(n: n) == true ? 1 : 0)
    test -= 1
}

 

 

728x90