如题 # include<stdio.h> # include<stdlib.h> # include<string.h> # include<math.h> unsigned long data[2000] ; int count; char str[20]; char str1[20]; char str2[20]; int cmp(const void *a,const void *b) { return *(unsigned long *)a-*(unsigned long *)b; } int issushu(unsigned long num) { unsigned long i,j ,k = num; for (j = (unsigned long)sqrt(k),i = 2;i <= j+1;i++) if (num%i==0) return 0; return 1; } void _itoah(int n,int digit ) { int i = 0; for(i = 0;i < digit;i ++) { //str[digit-i-1] = n%10 + '0'; str[digit-i-1] = n%10 + '0'; n /=10; } } void _itoad(int n,int digit,int b )// 针对 奇数和偶数的不同 增加一个标志位 { int i = 0,j = digit; if(b==1) digit ++; for(i = 0;i < j;i ++) { str[digit++] = n%10 + '0'; n /=10; } } void even_huiwen(int n) { long i ,j,digit = n/2,tmp = 0; //j = (long)pow(10,digit) -1; j = (long)pow(10,digit) -1; for(i = (long)pow(10,digit-1); i <= j;i++ ) //i代表偶数的高位//(long)pow(10,n-1); i <= j;i++ ) { _itoah(i,digit); _itoad(i,digit,0); str[n] = '/0'; strcpy(str1,str); strrev(str1); if (!strcmp(str1,str)) if(issushu(atol(str))) data[count ++] = atol(str);//printf("%ld/t",atol(str)),data[count ++] = atol(str); memset(str,NULL,sizeof(str)); memset(str1,NULL,sizeof(str1)); } } void odd_huiwen(int n) { long i ,j,k,digit = n/2,tmp = 0; j = (long)pow(10,digit) -1; for(k = 0;k < 10; k ++) // 奇数位中间的数字 for (i = (long)pow(10,digit-1);i <= j ; i ++) { _itoah(i,digit); str[digit] = k + '0'; _itoad(i,digit,1); str[n] = '/0'; strcpy(str1,str); strrev(str1); if (!strcmp(str1,str)) if(issushu(atol(str))) data[count ++] = atol(str);//printf("%ld/t",atol(str)),data[count ++] = atol(str); memset(str,NULL,sizeof(str)); memset(str1,NULL,sizeof(str1)); } } int main() { unsigned long a,i,j,x,y; data[0] = 2,data[1] = 3,data[2] = 5,data[3] = 7,data[4] = 11; //freopen("pprime1.out","w",stdout); count = 5; int n; //生产所有的回文素数 for(int _digit = 3; _digit <= 8;_digit+=2) { if(_digit%2==0) even_huiwen(_digit); else odd_huiwen(_digit); } qsort(data,count,sizeof(unsigned long ),cmp); scanf("%d",&n); while (n--) { scanf("%ld%ld",&x,&y); for (i=0;data[i] <=y&&i<=count;i++) { if (data[i]>=x) printf("%ld/n",data[i]); } } return 0; }