3 22 7 8
23 7 7
#include<bits/stdc++.h> using namespace std; #define ll long long const int inf = 0x3f3f3f3f; const int maxn = 1000 + 8; int t, n, tot, pri[maxn]; bool is[maxn]; void e() { tot = 0; memset(is, 1, sizeof(is)); is[0] = is[1] = 0; for(int i = 2; i < maxn; i++) { if(is[i]) { pri[++tot] = i; for(int j = i + i; j <= maxn; j += i) is[j] = 0; } } } int main() { // std::ios::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); e(); scanf("%d", &t); for(int i = 0; i < t; i++) { scanf("%d", &n); int m = upper_bound(pri + 1, pri + tot + 1, n) - pri; if(abs(pri[m] - n) > abs(pri[m - 1] - n)) printf("%d\n", pri[m - 1]); else printf("%d\n", pri[m]); } return 0; }
SDNU 1227.A great challenge.(埃氏筛)
原文:https://www.cnblogs.com/RootVount/p/11536495.html