洛谷习题P1217回文质数

前前后后修改六两个半小时
其实算不上难

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;
	}
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值