#10TLE,求助大佬

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

u_s_er_na_me @ 2023-08-23 19:11:33


#include <iostream>
using namespace std;
int reverse(int j)
{
    int sum = 0;
    while (j != 0)
    {
        sum = sum * 10 + j % 10;
        j /= 10;
    }
    return sum;
}
bool check(int a)
{
    for (int i = 2;i * i <= a;i++)
    {
        if (a % i == 0) return 0;
    }
    return 1;
}
int main()
{
    int a,b;
    cin >> a >> b;
    for (int i = a;i <= b;i++)
    {
        if (i == reverse(i) && check(i))
        cout << i << endl;
    }
}

by Igallta @ 2023-08-23 20:20:18

先判断是否是2的倍数以及是否是 2 会快很多。


上一页 |