请问各位大佬 第八个测试点是关于什么的呀 就差第八个测试点了

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

wll560 @ 2023-11-21 11:07:55

请问各位大佬 第八个测试点是关于什么的呀 就差第八个测试点了

#include<iostream>
#include<math.h>
using namespace std;
bool palin(int a)
{
    bool result = true;
    int digi[15], acount = 1;
    while (a != 0)
    {
        digi[acount++] = a % 10;
        a /= 10;
    }
    for (int j = 1; j <= acount / 2; j++)
    {
        if (digi[j] != digi[acount - j])
        {
            result = false;
            break;
        }
    }
    return result;
}

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

int main() {
    int left, right;
    cin >> left >> right;
    int i = left;
    i += ((i-1) % 2);
    for (i; i < right; i+= 2)
    {
        if (i > 1000000000)
            break;
        if (palin(i)&& prime(i))
        {
            cout << i << endl;
        }
    }
    return 0;
}

by paranoiaX @ 2023-11-22 10:52:42

请您注意,寻找质数的范围为[a, b]

for (i; i < right; i+= 2)
{
    if (i > 1000000000)
        break;
    if (palin(i)&& prime(i))
    {
        cout << i << endl;
    }
}

上述代码中

for (i; i < right; i+= 2)

改为

for (i; i <= right; i+= 2)

即可AC


by wll560 @ 2023-11-22 13:54:04

@paranoiaX 非常感谢!!!


by demo111 @ 2023-12-03 22:06:57

@wll560 大佬能问一下为什么要if (i > 1000000000) break;有这一步呢? i大于1000000000不是会自动结束吗


|