1. Title Description: Click to open the link
2. Solution thinking: According to test instructions know the largest prime number in the int range, you can first calculate the number of all primes within 1299709, and then two points to find the position of the prime number near N.
3. Code:
#define _crt_secure_no_warnings #include <iostream> #include <algorithm># include<string> #include <sstream> #include <set> #include <vector> #include <stack># include<map> #include <queue> #include <deque> #include <cstdlib> #include <cstdio># include<cstring> #include <cmath> #include <ctime> #include <functional>using namespace std;# Define N 1300000int vis[n];vector<int>primes;void init () {int m = sqrt (N + 0.5); for (int i = 2; I <= m;i++) if (!vi S[i]) for (int j = i*i; J < N; J + = i) vis[j] = 1;for (int i = 2; I <= n;i++) if (!vis[i]) primes.push_back (i);} int main () {//freopen ("test.txt", "R", stdin); int n;init (); while (scanf ("%d", &n)! = eof&&n) {if (!vis[n]) cout << 0 << endl;else{int L = 0, R = 100000;while (L < R) {int m = l + (r-l)/2;if (Primes[m] > N) r = M;else L = m + 1;} cout << Primes[l]-primes[l-1] << Endl;}} return 0;}
Exercise 10-4 Prime Interval UVa1644