求助!超时

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

CCCcccc0512 @ 2024-03-18 09:29:55

#include <stdio.h>
int zhi(int a)
{
    int re=0;
    for(int i=2;i<a;i++){
        if(a%i==0){
            re=1;
        }
    }
    return re;
}
int hui(int a)
{
    int n=a;
    int intages=0,intall=0;
    int re=1;
    while(n!=0){
        intages=n%10;
        intall=intall*10+intages;
        n/=10;
    }
    if(intall==a){
        re=0;
    }
    return re;

}

int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    for(int i=a;i<=b;i++){
        if(zhi(i)==0){
            if(hui(i)==0){
                printf("%d\n",i);
            }
        }
    }

    return 0;
}

by SHUYONGRUI @ 2024-04-06 10:58:37

#include<bits/stdc++.h>
using namespace std;
int Palindromes(int n){
    if(n<10)return 1;
    int nn=n,he=0;
    while(nn){
        he=he*10+nn%10;
        nn/=10;
    }
    if(he==n)return 1;
    else return 0;
}
int prime(int x){
    for(int i=2;i<=sqrt(x);i++)if(x%i==0) return 0;
    return 1;
}
int a,b;
int main(){
cin>>a>>b;
    for(int i=a;i<=b;i++){
        if(Palindromes(i) && prime(i)){
            cout<<i<<endl;
        }
    }
return 0;
}

|