这个题比较直观,就是生成回文数和素数的判断,因为懒得想解法,就直接生成了回文数,所以代码有点长……长…… /* ID: xincaor1 PROG: pprime LANG: C++ */ #include <cstdio> #include <iostream> using namespace std; int prime[1300],c,a,b; void calPrime() { prime[0]=2; int i,j; c=1; for(i=2;i<=10000;i++) { for(j=0;j<c;j++) if(i%prime[j]==0) break; if(j==c) prime[c++]=i; } } bool judgePrime(int x) { for(int i=0;i<c;i++) { if(x<=prime[i]) return 1; if(x%prime[i]==0) return 0; } return 1; } void calPalindrome(int x) { switch(x) { case 1: for(int p1=1;p1<=9;p1+=2) if(p1>=a&&p1<=b&&judgePrime(p1)) cout<<p1<<endl; break; case 2: for(int p1=1;p1<=9;p1+=2) { int x; x=p1*10+p1; if(x>=a&&x<=b&&judgePrime(x)) cout<<x<<endl; } break; case 3: for(int p1=1;p1<=9;p1+=2) for(int p2=0;p2<=9;p2++) { int x; x=p1+p2*10+p1*100; if(x>=a&&x<=b&&judgePrime(x)) cout<<x<<endl; } break; case 4: for(int p1=1;p1<=9;p1+=2) for(int p2=0;p2<=9;p2++) { int x; x=p1+p2*10+p2*100+p1*1000; if(x>=a&&x<=b&&judgePrime(x)) cout<<x<<endl; } break; case 5: for(int p1=1;p1<=9;p1+=2) for(int p2=0;p2<=9;p2++) for(int p3=0;p3<=9;p3++) { int x; x=p1+p2*10+p3*100+p2*1000+p1*10000; if(x>=a&&x<=b&&judgePrime(x)) cout<<x<<endl; } break; case 6: for(int p1=1;p1<=9;p1+=2) for(int p2=0;p2<=9;p2++) for(int p3=0;p3<=9;p3++) { int x; x=p1+p2*10+p3*100+p3*1000+p2*10000+p1*100000; if(x>=a&&x<=b&&judgePrime(x)) cout<<x<<endl; } break; case 7: for(int p1=1;p1<=9;p1+=2) for(int p2=0;p2<=9;p2++) for(int p3=0;p3<=9;p3++) for(int p4=0;p4<=9;p4++) { int x; x=p1+p2*10+p3*100+p4*1000+p3*10000+p2*100000+p1*1000000; if(x>=a&&x<=b&&judgePrime(x)) cout<<x<<endl; } break; case 8: for(int p1=1;p1<=9;p1+=2) for(int p2=0;p2<=9;p2++) for(int p3=0;p3<=9;p3++) for(int p4=0;p4<=9;p4++) { int x; x=p1+p2*10+p3*100+p4*1000+p4*10000+p3*100000+p2*1000000+p1*10000000; if(x>=a&&x<=b&&judgePrime(x)) cout<<x<<endl; } } } int main() { freopen("pprime.in","r",stdin); freopen("pprime.out","w",stdout); calPrime(); cin>>a>>b; int p,q,t; p=q=0; t=a; while(t>0) { p++; t/=10; } t=b; while(t>0) { q++; t/=10; } for(int i=p;i<=q;i++) { calPalindrome(i); } return 0; }