c++最后一个测试点超时,求助

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

a13901280570 @ 2024-01-24 23:04:01

#include <iostream>
#include <cmath>

using namespace std;

int f(int n) {
    int s = 0;
    int k = n;
    while (n != 0) {
        s = s * 10 + n % 10;
        n /= 10;
    }
    return (s == k);
}

int g(int n) {
    if (n == 1)
        return 0;
    if (n % 2 == 0)
        return 0;
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0)
            return 0;
    }
    return 1;
}

int main() {
    int a, b;
    cin >> a >> b;
    for (int i = a; i <= b; i++) {
        if (f(i) && g(i)) {
            cout << i << endl;
        }
    }
    return 0;
}

by xueshengyi @ 2024-01-26 11:24:09

质数用筛法求


by wpc_120318 @ 2024-01-30 16:11:12

直接取b和9989899的最小值


|