回文素数

来源:互联网 发布:五大流氓国笑话知乎 编辑:程序博客网 时间:2024/04/28 15:12


Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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

5711101131151181191313353373383
 

#include <iostream>#include <cmath> using namespace std;int Prime(long int w) {   int a, b,c;   a = w%2;   b=3;   c=(int)sqrt((double)w);   while (a&&b<=c)   {     a=w%b;     b+=2;   }   return a; } int main() {   int a,b,c,d,e;   int m,n;   long int w;   while(cin >> m >> n)  {  a=0;   b=0;   c=0;   d=0;   e=4;        while (a<=9)        {          e++;          if (e==10)          {            e=0;            d++;          }          if (d==10)          {            d=0;            c++;          }          if (c==10)          {            c=0;            b++;          }          if (b==10)          {            b=0;            a++;          }         w=a*100000000+b*10000000+c*1000000+d*100000+e*10000+d*1000+c*100+b*10+a;          while (w%10==0)          {           w=w/10;          }          if( w >= m && w < n )         {              if (Prime(w))              {                cout << w << endl;               if (w>5&&w<100)                {                  cout << 11 <<endl;               }              }          }        }       cout << endl;  } return 0; }



第二代码,西西里上的题,输入0 0时结束程序

#include<iostream>#include<cstdio>#include<cmath>using namespace std;const int maxn=10000;int a,b;int ans[maxn];bool ask(int x){    for (int i=2; i<=sqrt(x); i++)        if (x%i==0) return false;    return true;}void prepare(){    for (int i=1; i<=maxn; i++){        int state=i,tmp=i/10;        while (tmp!=0){            state=state*10+tmp%10;            tmp/=10;        }        if (ask(state)) ans[++ans[0]]=state;        if (state==7) ans[++ans[0]]=11;    }}inline int init(){    while (scanf("%d%d",&a,&b)!=EOF){          if(a==0&&b==0) return 0;           for (int i=1; i<=ans[0]; i++)                if ((ans[i]>=a) && (ans[i]<=b)) printf("%d\n",ans[i]);    }    return 0;}int main(){    prepare();    init();    return 0;}


法3:

bool checkPri(int n){bool b=true;for(int i=2;i<=n/2;i++){if(n%i==0){b=false;break;}}return b;}


原创粉丝点击