반응형
문제
https://www.acmicpc.net/problem/6588
풀이
에라토스테네스의 체를 이용해서 1000000 이하의 소수를 먼저 구한다. 에라토스테네스의 체를 이용하면 c 배열에 각 수가 소수인지 아닌지에 대한 정보가 저장이 된다.
주어진 짝수 n이 두 개의 홀수 소수로 이루어지는지 알아보기 위해서, i = 3 부터 2칸씩 증가시키면 된다. 이때 i와 (n - i) 둘 다 소수일 때 정답이 된다.
코드
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
36
37
38
39
40
|
#include <iostream>
using namespace std;
const int MAX = 1000000;
int n;
bool c[MAX + 1];
int main()
{
cin.tie(0);
ios::sync_with_stdio(0);
for (int i=2; i<=MAX; i++)
{
if (!c[i])
for (int j=i+i; j<=MAX; j+=i) c[j] = true;
}
while(true)
{
cin >> n;
if (!n) break;
bool fail = true;
for (int i=3; i<MAX; i+=2)
{
if (!c[i] && !c[n - i])
{
cout << n << " = " << i << " + " << n - i << '\n';
fail = false;
break;
}
}
if (fail) cout << "Goldbach's conjecture is wrong." << '\n';
}
return 0;
}
|
cs |
반응형
'BOJ' 카테고리의 다른 글
백준 1874 - 스택 수열 (0) | 2020.02.24 |
---|---|
백준 2004 - 조합 0의 개수 (0) | 2020.02.18 |
백준 2346 - 풍선 터뜨리기 (0) | 2020.01.25 |
백준 1753 - 최단경로 (1) | 2020.01.10 |
백준 1197 - 최소 스패닝 트리 (0) | 2019.12.26 |