본문 바로가기

Algorithm/Math

[C++] 백준 6588번 : 골드바흐의 추측 (S1)

#문제

www.acmicpc.net/problem/6588

 

6588번: 골드바흐의 추측

각 테스트 케이스에 대해서, n = a + b 형태로 출력한다. 이때, a와 b는 홀수 소수이다. 숫자와 연산자는 공백 하나로 구분되어져 있다. 만약, n을 만들 수 있는 방법이 여러 가지라면, b-a가 가장 큰

www.acmicpc.net

#풀이 & 학습한 내용

ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); 이 없으면 시간초과가 나는 문제이다. 그리고 문제를 풀 때는 supremo7.tistory.com/116에서 이용한 에라토스테네스의 체를 통해 해결한다.

 

#소스코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<algorithm>
 
using namespace std;
 
bool prime[1000002];//지워졌으면 true(1), 소수인게 false(0)
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
 
    prime[1= true;
    for (int i = 2; i*<= 1000000; i++) {
        if (prime[i] == false) {
            for (int j = i * 2; j <= 1000000; j+=i) {
                prime[j] = true;
            }
        }
    }
 
    int num;
    while (1) {
        cin >> num;
        if (num == 0)
            break;
        for (int i = 3; i <= num / 2; i += 2) {
            if (prime[i] == false && prime[num - i] == false) {
                cout << num << " = " << i << " + " << num - i << '\n';
                break;
            }
        }
    }
 
}
cs

 

github.com/HoYoungChun/BOJ_algorithm/blob/master/Math/BOJ_6588.cpp

 

HoYoungChun/BOJ_algorithm

Baekjoon Online Judge problem solving by C++. Contribute to HoYoungChun/BOJ_algorithm development by creating an account on GitHub.

github.com