#9 TLE 终于过了(跪了)

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

LotusXu @ 2024-04-21 16:42:23

#include <iostream>
#include <cmath>

using namespace std;

bool isPrim(int n);

bool isHui(int n);

int main()
{
    int a = 0;
    int b = 0;
    int hui[50000] = { 0 };
    int len = 0;

    cin >> a >> b;

    for (int i = a;i <= b;++i)
    {
        if (i % 2 != 0 && isHui(i))
        {
            hui[len++] = i;
        }
    }

    for (int i = 0;i < len;++i)
    {
        if (isPrim(hui[i]))
        {
            cout << hui[i] << endl;
        }
    }
}

bool isPrim(int n)
{
    for (int i = 2;i <= sqrt(n);++i)
    {
        if (n % i == 0)
        {
            return false;
        }
    }

    return true;
}

bool isHui(int n)
{
    int a[20] = { 0 };
    int index = 0;

    if (n == 11)
    {
        return true;
    }

    while (n > 0)
    {
        a[index++] = n % 10;
        n = n / 10;
    }

    if (index % 2 == 0)
    {
        return false;
    }

    for (int i = 0;i <= index / 2;++i)
    {
        if (a[i] != a[index - i - 1])
        {
            return false;
        }
    }

    return true;
}

by XuYueming @ 2024-04-21 17:11:00

@LotusXu jbl


|