66分,挺吉利的(,TLE,求大佬指点

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

BreadC @ 2024-11-17 20:42:33

#include<iostream>
using namespace std;
bool pallindrome(int x)
{
    int y = 0,num;
    num = x;
    while (x > 0)
    {
        y = y * 10 + x % 10;
        x /= 10;
    }
    if (num == y)
        return 1;
    else return 0;
}
bool prime(int x)
{
    int fac = 0;
    if (x == 2)
        return 1;
    else
    {
        for (int i = 2; i < x; i++)
        {
            if (x % i == 0)
                return 0;
        }
        return 1;
    }
}
int main()
{
    int a, b;
    cin >> a >> b;
    for (int i = a; i <= b; i++)
    {
        if (pallindrome(i) == 1 && prime(i) == 1)
            cout << i << endl;
    }
    return 0;
}

by 11514zbs @ 2024-11-17 20:44:57

#include<iostream>
using namespace std;
bool pallindrome(int x)
{
    int y = 0,num;
    num = x;
    while (x > 0)
    {
        y = y * 10 + x % 10;
        x /= 10;
    }
    if (num == y)
        return 1;
    else return 0;
}
bool prime(int x)
{
    int fac = 0;
    if (x == 2)
        return 1;
    else
    {
        for (int i = 2; i <= x / i; i++)//改了这里
        {
            if (x % i == 0)
                return 0;
        }
        return 1;
    }
}
int main()
{
    int a, b;
    cin >> a >> b;
    for (int i = a; i <= b; i++)
    {
        if (pallindrome(i) == 1 && prime(i) == 1)
            cout << i << endl;
    }
    return 0;
}

by 11514zbs @ 2024-11-17 20:45:22

@BreadC


by BreadC @ 2024-11-17 20:53:02

@11514zbs收到,谢谢大佬!


|