66分求调!3个TLE!玄关

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

chenkuan_001 @ 2024-08-24 16:14:24

代码

#include<bits/stdc++.h>
using namespace std;
int check(int a){
    int s[15];
    for(int i=2;i*i<=a;i++){
        if(a%i==0){
            return 0;
        }
    }
    int lc=0;
    while(a!=0){
        lc++;
        s[lc]=a%10;
        a/=10;
    }
    for(int i=1;i<=lc/2;i++){
        if(s[i]!=s[lc-i+1]){
            return 0;
        }
    }
    return 1;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int a,b;
    cin>>a>>b;
    for(int i=a;i<=b;i++){
        if(check(i)==1){
            cout<<i<<endl;
        }
    }
    return 0;
}

|