Special Prime number problem:1151Time Limit:1000msMemory Limit:65535KDescription
If a number is prime, and his number in the mass tables is prime, it is called a special prime. For example, the 7th prime number in the quality tables is 17, because 7 and 17 are prime numbers, so we call 17 a special prime number, such as the 10th prime number is 29, because 10 is not a prime number, so 29 is not a special prime number. Now given a nonnegative integer n, the largest special prime number of the <=n is obtained.
Input
Multiple sets of input data n (0<=n<=10^6).
Output
The output is the largest special prime number less than or equal to N, and for a given n if there is no such prime number, output-1.
Sample Input
0205060
Sample Output
-1174159
Hint Source
#include <iostream>#include<string.h>using namespacestd;#defineMAXN 1000100inti,j,n,m,a[maxn],b[maxn],k=1;voidGetprime ()//Prime number of hit table get prime number{memset (A,0,sizeof(a)); memset (b,0,sizeof(b)); b[1]=1; for(i=2; i<maxn;i++) { if(!B[i]) {A[k++]=i; for(j=i+i;j<maxn;j+=i) b[j]=1; } }}intMain () {getprime (); while(cin>>N) {if(n<3) {cout<<-1<<Endl; Continue; } intflag=3; for(i=1; i<k;i++) { if(!b[i])//It's a special prime number. { if(a[i]<=n)//mark the previous oneFlag=a[i];//you can't just output the last special prime number, you can't run to else. Else Break; Note that this must not be output in else, because the last special prime number will not run into else.
I found this error with the generator in the hall, 1000 input 999 sets of output
If you want to output in else, change the MAXN to 2000100}} cout<<flag<<Endl; } /*for (i=1;i<20;i++) {if (!b[i]) cout<< "Special prime number:"; else cout<< ""; cout<< "I:" <<i<< "" <<a[i]<<endl; }*/ return 0;}
Nefu 1151 Special prime numbers