66pts3TLE求调必关!

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

Yangxixuan @ 2024-10-17 19:17:54

#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
using namespace std;
int a,b;
char s[15];
bool isPrime(int n){
    for(int i=2;i<=sqrt(n);i++){
        if(n%i==0){
            return false;
        }
    }
    return true;
}
bool isPal(int n){
    int i=0;
    if(n%10==n){
        return true;
    }
    while(n!=0){
        s[i]=char(n%10+'0');
        n/=10;
        i++;
    }
    for(int t=0;t<=strlen(s)/2-1;t++){
        if(s[t]!=s[strlen(s)-t-1]){
            return false;
        }
    }
    return true;
}
int main(){
    scanf("%d%d",&a,&b);
    for(int i=a;i<=b;i++){
        if(isPrime(i)&&isPal(i)){
            printf("%d\n",i);
        }
    }
    return 0;
}

by liruizhou_lihui @ 2024-10-17 19:21:50

@liruizhou_lihui

你擅长打磨你,你来


by liruizhou_lihui @ 2024-10-17 19:22:38

@Yangxixuan 你的回文数判断有问题,可以写简单一点


by liruizhou_lihui @ 2024-10-17 19:24:16

@Yangxixuan 用一个 sum 备份,每次求数的个位,然后 \bmod 10,摸到0为止


by liruizhou_lihui @ 2024-10-17 19:26:43

@Yangxixuan 先判断回文数,复杂度低,如果不成立直接短路


by Yangxixuan @ 2024-10-18 16:56:38

大佬,实力!@liruizhou_lihui


|