素数回文 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16056 Accepted Submission(s): 3564
Problem Description xiaoou33对既是素数又是回文的数特别感兴趣。比如说151既是素数又是个回文。现在xiaoou333想要你帮助他找出某个范围内的素数回文数,请你写个程序找出 a 跟b 之间满足条件的数。(5 <= a < b <= 100,000,000);
Input 这里有许多组数据,每组包括两组数据a跟b。
Output 对每一组数据,按从小到大输出a,b之间所有满足条件的素数回文数(包括a跟b)每组数据之后空一行。
Sample Input 5 500
Sample Output 5 7 11 101 131 151 181 191 313 353 373 383
Author xiaoou333
Source |
1 #include <stdio.h> 2 #include <stdio.h> 3 using namespace std; 4 bool a[9989900]; 5 int prime[1005]; 6 void set() 7 { 8 int i,j; 9 for(i = 2; i<=9989899; i+=2) 10 a[i] = true; 11 for(i = 3; i<=3161; i++){ 12 if(a[i]) 13 continue; 14 for(j = i+i; j<=9989899; j+=i) 15 a[j] = true; 16 } 17 } 18 int huiwen(int n) 19 { 20 int a = 0,b = n; 21 int r; 22 while(b){ 23 r = b%10; 24 a = a*10+r; 25 b/=10; 26 } 27 if(a == n) 28 return 1; 29 return 0; 30 } 31 int main() 32 { 33 int n,m,i,k = 2; 34 set(); 35 prime[0] = 5; 36 prime[1] = 7; 37 for(i = 11; i<=9989899; i+=2){ 38 if(!a[i] && huiwen(i)) 39 prime[k++] = i; 40 } 41 while(~scanf("%d%d",&n,&m)){ 42 for(i = 0; i<k; i++){ 43 if(prime[i]<n) 44 continue; 45 else if(prime[i]>m) 46 break; 47 else 48 printf("%d\n",prime[i]); 49 } 50 printf("\n"); 51 } 52 return 0; 53 }