有佬可以帮忙看下吗 最后3个点TLE

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

ADmark42 @ 2023-10-31 19:54:45

#include <stdio.h>
#include <stdbool.h>
#include <math.h>

bool zhi(int a) {
    if (a <= 1 || a % 2 == 0)
        return false;
    for (int i = 3; i <= sqrt(a); i += 2) {
        if (a % i == 0) {
            return false;
        }
    }
    return true;
}

bool hui(int a) {
    int g[10], i = 0;
    while (a > 0) {
        g[i] = a % 10;
        a /= 10;
        i++;
    }
    for (int j = 0; j < i; j++)
        if (g[j] != g[i - j - 1]) {
            return false;
        }
    return true;
}

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

by lym12 @ 2023-10-31 20:05:40

质数判断炸了,用埃拉托尼斯筛法,快一点,可以自行了解 @ADmark42


by Custlo0793 @ 2023-10-31 20:07:45

TwistZz 好评。


by ADmark42 @ 2023-11-01 12:54:25

@Mayile1022 ok谢谢佬


by ADmark42 @ 2023-11-01 12:54:51

@Cust10 好好好


|