首页 > 代码库 > hihocoder offer收割编程练习赛12 A 歌德巴赫猜想

hihocoder offer收割编程练习赛12 A 歌德巴赫猜想

思路:

枚举。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int MAX_N = 1000005;
 7 
 8 int prime[MAX_N];
 9 bool is_prime[MAX_N + 1];
10 
11 int init(int n)
12 {
13     int p = 0;
14     for (int i = 0; i <= n; i++)
15         is_prime[i] = true;
16     is_prime[0] = is_prime[1] = false;
17     for (int i = 2; i <= n; i++)
18     {
19         if (is_prime[i])
20         {
21             prime[p++] = i;
22             for (int j = 2 * i; j <= n; j += i)
23                 is_prime[j] = false;
24         }
25     }
26     return p;
27 }
28 
29 int main()
30 {
31     int num = init(1000000);
32     int n;
33     cin >> n;
34     for (int i = 0; i < num; i++)
35     {
36         if (binary_search(prime + i, prime + num, n - prime[i]))
37         {
38             int index = lower_bound(prime + i, prime + num, n - prime[i]) - prime;
39             cout << prime[i] << " " << prime[index] << endl;
40             break;
41         }
42     }
43     return 0;
44 }

 

hihocoder offer收割编程练习赛12 A 歌德巴赫猜想