#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;
}