前前后后修改六两个半小时
其实算不上难
ac代码
#include<bits/stdc++.h>
using namespace std;
//根据题目的回文数定义,易知在100以内,回文质数只有5 7 11
//所以生成回文数从三位开始
int Num[800];
int step=3;
int flag(int temp){//判断素数,素数返回这个数
int t=0;
for(int i=2;i<=sqrt(temp);i++){
if(temp%i==0)t++;
}
if(t==0)return temp;else return 0;
}
int creat3N(){//生成三位回文数
int d1,d2,temp;
for(d1=1;d1<=9;d1+=2){
for(d2=0;d2<=9;d2++){
temp=100*d1+10*d2+d1;
temp=flag(temp);
if(temp!=0){
Num[step]=temp;
step++;
}
}
}
}
int creat4N(){//生成四位回文数
int d1,d2,temp;
for(d1=1;d1<=9;d1+=2){
for(d2=0;d2<=9;d2++){
temp=1000*d1+100*d2+10*d2+d1;
temp=flag(temp);
if(temp!=0){
Num[step]=temp;
step++;
}
}
}
}
int creat5N(){//生成五位回文数
int d1,d2,d3,temp;
for(d1=1;d1<=9;d1+=2){
for(d2=0;d2<=9;d2++){
for(d3=0;d3<=9;d3++){
temp=10000*d1+1000*d2+100*d3+10*d2+d1;
temp=flag(temp);
if(temp!=0){
Num[step]=temp;
step++;
}
}
}
}
}
int creat6N(){//生成六位回文数
int d1,d2,d3,temp;
for(d1=1;d1<=9;d1+=2){
for(d2=0;d2<=9;d2++){
for(d3=0;d3<=9;d3++){
temp=100000*d1+10000*d2+1000*d3+100*d3+10*d2+d1;
temp=flag(temp);
if(temp!=0){
Num[step]=temp;
step++;
}
}
}
}
}
int creat7N(){//生成七位回文数
int d1,d2,d3,d4,temp;
for(d1=1;d1<=9;d1+=2){
for(d2=0;d2<=9;d2++){
for(d3=0;d3<=9;d3++){
for(d4=0;d4<=9;d4++){
temp=1000000*d1+100000*d2+10000*d3+1000*d4+100*d3+10*d2+d1;
temp=flag(temp);
if(temp!=0){
Num[step]=temp;
step++;
}
}
}
}
}
}
int creat8N(){//生成八位回文数
int d1,d2,d3,d4,temp;
for(d1=1;d1<=9;d1+=2){
for(d2=0;d2<=9;d2++){
for(d3=0;d3<=9;d3++){
for(d4=0;d4<=9;d4++){
temp=10000000*d1+1000000*d2+100000*d3+10000*d4+1000*d4+100*d3+10*d2+d1;
temp=flag(temp);
if(temp!=0){
Num[step]=temp;
step++;
}
}
}
}
}
}
int main(){
int a,b;
cin>>a>>b;
Num[0]=5;Num[1]=7;Num[2]=11;
creat3N();
creat4N();
creat5N();
creat6N();
creat7N();
creat8N();
for(int cnt=0;cnt<800;cnt++){
if(Num[cnt]>=a&&Num[cnt]<=b){
cout<<Num[cnt]<<endl;
}else if(Num[cnt]>b)break;
}
}