Libingyue2011 @ 2022-12-31 12:17:43
三 WA 无超时
#include<bits/stdc++.h>
using namespace std;
bool prime(int x){
if(x==1) return 0;
for(int i=2;i*i<=x;i++){
if(x%i==0) return 0;
}
return 1;
}
int m,n;
bool f(int x){
return (x>=m)&&(x<=n);
}
int main() {
scanf("%d %d",&m,&n);
for(int i=5;i<=9;i+=2){//一位数
if(f(i)==1&&prime(i)==1) printf("%d\n",i);
}
if(f(11)==1) printf("11\n");//两位数
for(int i=1;i<=9;i+=2){//三位数
for(int j=0;j<=9;j++){
if(f(101*i+10*j)==1&&prime(101*i+10*j)==1) printf("%d\n",101*i+10*j);
}
}
for(int i=1;i<=9;i+=2){//四位数
for(int j=1;j<=9;j++){
if(f(1001*i+110*j)==1&&prime(1001*i+10*j)==1) printf("%d\n",1001*i+10*j);
}
}
for(int i=1;i<=9;i+=2){//五位数
for(int j=0;j<=9;j++){
for(int k=0;k<=9;k++){
if(f(10001*i+1010*j+100*k)==1&&prime(10001*i+1010*j+100*k)==1)
printf("%d\n",10001*i+1010*j+100*k);
}
}
}
for(int i=1;i<=9;i+=2){//六位数
for(int j=0;j<=9;j++){
for(int k=0;k<=9;k++){
if(f(100001*i+1001*j+11*k)==1&&prime(100001*i+1001*j+11*k)==1)
printf("%d\n",100001*i+1001*j+11*k);
}
}
}
for(int i=1;i<=9;i+=2){//七位数
for(int j=0;j<=9;j++){
for(int k=0;k<=9;k++){
for(int l=0;l<=9;l++){
if(f(1000001*i+100010*j+10100*k+1000*l)==1&&prime(1000001*i+100010*j+10100*k+1000*l)==1)
printf("%d\n",1000001*i+100010*j+10100*k+1000*l);
}
}
}
}
for(int i=1;i<=9;i+=2){//八位数
for(int j=0;j<=9;j++){
for(int k=0;k<=9;k++){
for(int l=0;l<=9;l++){
if(f(10000001*i+1000010*j+100100*k+11000*l)==1&&prime(10000001*i+1000010*j+100100*k+11000*l)==1)
printf("%d\n",10000001*i+1000010*j+100100*k+11000*l);
}
}
}
}
return 0;
}
by Libingyue2011 @ 2022-12-31 12:18:19
没有 TLE 哟
by _Peter_ @ 2022-12-31 14:08:02
#include <iostream>
#include <cmath>
using namespace std;
bool isZhiShu( int x ){
bool f = true;
int sqrtx=sqrt(x);
for( int i = 2 ; i <= sqrtx ; i++ ){
if( x % i == 0 ){
f = false;
return false;
}
}
return true;
}
bool isHuiWen( int x ){
int x_old = x, sum = 0, xj = x;
//把x反过来,判断x是否与之前的x一样
while( x_old != 0 ){
x = x_old;
x %= 10;
sum = (sum + x ) * 10;
x_old /= 10;
}
sum /= 10;
if( sum == xj ){
return true;
}
return false;
}
int main()
{
int a, b;
cin >> a >> b;
if( a % 2 == 0 ){
a++;
}
for( int i = a ; i <= b ; i += 2 ){
if( isHuiWen(i) && isZhiShu(i) ){
cout << i << endl;
}
}
return 0;
}
by Lucas2024 @ 2023-01-25 15:50:25
#include<iostream>
#include<cmath>
using namespace std;
int m,n,pal[100000005],pos=4;
bool isprime(int x)
{
int i;
for(i=2;i<=sqrt(x);i++)
{
if(x%i==0)
{
return false;
break;
}
}
return true;
}
int main()
{
int a,b,c,d,e,i;
cin>>m>>n;
pal[1]=5;pal[2]=7;pal[3]=11;
for(a=1;a<=9;a+=2)/*只有奇数是素数*/
{
for(b=0;b<=9;b++)
{
pal[pos++]=101*a+10*b;
}
}//三位
for(a=1;a<=9;a+=2)
{
for(b=0;b<=9;b++)
{
for(c=0;c<=9;c++)
{
pal[pos++]=10001*a+1010*b+100*c;
}
}
}//五位
for(a=1;a<=9;a+=2)
{
for(b=0;b<=9;b++)
{
for(c=0;c<=9;c++)
{
for(d=0;d<=9;d++)
{
pal[pos++]=1000001*a+100010*b+10100*c+1000*d;
}
}
}
}//七位
for(a=1;a<=9;a+=2)
{
for(b=0;b<=9;b++)
{
for(c=0;c<=9;c++)
{
for(d=0;d<=9;d++)
{
for(e=0;e<=9;e++)
{
pal[pos++]=100000001*a+10000010*b+1000100*c+101000*d+10000*e;
}
}
}
}
}//九位
/*构造回文数*/
for(i=1;pal[i]<=n;i++)
{
if(pal[i]>=m&&isprime(pal[i]))
{
cout<<pal[i]<<endl;
}
}
return 0;
}