3 条题解
-
0
#include <algorithm> #include <iostream> using namespace std; const int N = 1e5 + 5; bool vis[N]; int ans[N]; int cnt = 0; void shai(){ for(int i = 2; i <= N; i++){ if(vis[i] == 0){ ans[++cnt] = i; for(int j = i+i; j <= N; j+=i){ vis[j] = 1; } } } } int main(){ shai(); int t; cin >> t; while(t--){ int x; cin >> x; if(x==1){ // 特判 n==1 cout << 2 << endl; } else if(vis[x] == 0){ // 是素数直接输出 cout << x << endl; }else{ int left = 0, right = 0; // 找比x小的最大素数left,比x大的最小素数right for(int i = 1; i <= cnt; i++){ if(ans[i] < x){ left = ans[i]; } if(ans[i] > x){ right = ans[i]; break; } } if(right - x > x - left){ cout << left << endl; }else if(right - x <= x - left){ cout << right << endl; } } } }
信息
- ID
- 179
- 时间
- 3000ms
- 内存
- 128MiB
- 难度
- 6
- 标签
- (无)
- 递交数
- 702
- 已通过
- 201
- 上传者