closest 素数
View Code
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> #include<vector> #include<string> #include<math.h> #include<map> #include<set> #include<algorithm> using namespace std; int pri[3200]; bool visit[3200] = {0}; int prime( ) { visit[1] = visit[0] = 1; int x = 50; for( int i = 2; i <= x; i++) { if( !visit[i] ) { for( int j = i + i; j <= 1100; j += i ) { visit[j] = 1; } } } int ans = 0; for( int i = 1; i <= 1100 ; i++) if( !visit[i] ) pri[ans++] = i; return ans; } int Max(int x1,int x2, int x) { int a = abs(x1-x); int b = abs(x2-x); if( a == b ) return x1; else if( a < b ) return x1; else return x2; } int jugde( int x , int num) { int id = lower_bound(pri, pri + num , x ) - pri; int x1 = pri[id]; if( x1 == x ) return x; else { if(x > 0 ) { int x2 = pri[id-1]; return Max(x1,x2,x); } else return x1; } } int main( ) { int N, T, num; num = prime( ); scanf("%d",&T); while(T--) { scanf("%d",&N); printf("%d\n",jugde(N,num)); } return 0; }
posted on 2012-08-05 21:05 more think, more gains 阅读(132) 评论(0) 编辑 收藏 举报