大佬求助,#7#8#9都是TLE怎么办啊!

P1217 [USACO1.5] 回文质数 Prime Palindromes

202240210226a @ 2023-08-25 18:36:17

#include<iostream>
#include<cstdio>
#include<cmath>
int isPrime(int n);
int IsHuiWenShu(int num);
int main()
{
    int a,b,num=0;
    scanf("%d%d",&a,&b);
    if(a%2==0){
        a++;
    }
    for(int i=a;i<=b;i++){
        if(isPrime(i)==1&&IsHuiWenShu(i)==1){
            printf("%d\n",i);
        }
    }
    return 0;
}
int isPrime(int n)
{
    int i,limit;
    limit=sqrt(n)+1;
    if(n<=1){
        return 0;
    }
    if(n==2){
        return 1;
    }
    if(n>2){
        for(i=2;i<limit;i++){
            if(n%i==0){
                return 0;
            }
        }
        if(i>=limit){
            return 1;
        }
    }
}
int IsHuiWenShu(int num)
{
    int n = num;
    int temp=0;
    while(num!=0)
    {
        temp = temp*10 + num%10;  
        num = num/10;
    }
    if(n == temp)
    {
        return 1;
    }
        else
    {
        return 0;
    }
}

by 123WRz @ 2023-10-06 14:25:16

判断位数,不判断会超时的\

if(n>=10 && n<=99 && n!=11 || n>=1000 && n<=9999)return false;
if(n>=100000 && n<=999999 || n>=10000000 && n<=99999999)return false;
return true;

应该能ac


|