개인공부/코딩테스트
백준 9020번: 골드바흐의 추측
Itsumo
2023. 1. 30. 14:53
9020번: 골드바흐의 추측
1보다 큰 자연수 중에서 1과 자기 자신을 제외한 약수가 없는 자연수를 소수라고 한다. 예를 들어, 5는 1과 5를 제외한 약수가 없기 때문에 소수이다. 하지만, 6은 6 = 2 × 3 이기 때문에 소수가 아
www.acmicpc.net
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#include <iostream>
using namespace std;
bool Arr[10000] = { true, true };
int FindPrevPrimeNum(int num)
{
int ReturnNum = num-1;
for (int i = ReturnNum; ; --ReturnNum)
{
if (Arr[ReturnNum] == false)
return ReturnNum;
}
return 0;
}
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
int TestCase = 0;
cin >> TestCase;
for (int i = 2; i < 10000; ++i)
{
if (Arr[i] == false)
{
for (int j = 2; i * j <= 10000; ++j)
{
Arr[i * j] = true;
}
}
}
for (int i = 0; i < TestCase; ++i)
{
int Number = 0;
cin >> Number;
int Left = Number / 2;
int Right = Number / 2;
while (Arr[Right] != false)
{
Left = FindPrevPrimeNum(Left);
if (Arr[Number - Left] == false)
Right = Number - Left;
}
cout << Left << " " << Right << "\n";
}
return 0;
}
|
cs |