56分,help!

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

MC_dream_tsr @ 2024-08-28 14:29:35


# include<bits/stdc++.h>
using namespace std;
bool primse(int n){
    if(n < 2) return 0;
    for(int i = 2; i * i <= n; i++)
        if(n % i == 0) return 0;
    return 1;
}
bool hw(int x){
    int t =  x, n = 0;
    while(x > 0) {
        n = n * 10 + x % 10;
        x /= 10;
    }
    if(t == n) return 1;
    else return 0;
}
int main(){
    int a, b;
    cin >> a >> b;
    if(b > 9999999) b = 9999999;
    for(int i = a; i <= b; i += 2) {
        if(hw(i)){
            if(primse(i)) cout << i << endl;
        }
    }
    return 0;
}

by 初星逝者 @ 2024-08-28 14:40:44

@MC_dream_tsr 显然不用+2


by New__bee @ 2024-08-28 14:49:54

@MC_dream_tsr
楼主,您要确定 a 为奇数。


by New__bee @ 2024-08-28 14:51:08

@MC_dream_tsr


by zhaoyonghao @ 2024-08-28 14:52:29

@MC_dream_tsr 这样写可以卡评测 但是本题主要靠构造法模拟

#include <bits/stdc++.h>
using namespace std;

int a, b;

int daos(int n)
{
    int dao = 0;
    while (n)
    {
        dao = dao * 10 + n % 10;
        n /= 10;
    }
    return dao;
}

bool pss(int n)
{
    if (n % 2 == 0) return 0;
    int t = sqrt(n);
    for (int i = 3; i <= t; i += 2)
        if (n % i == 0) return 0;
    return 1;
}

int main()
{
    scanf("%d%d", &a, &b);
    if (b % 2 == 0) b--;
    if (a % 2 == 0) a++;
    for (int i = a; i <= b; i += 2)
    {
        if (daos(i) == i && pss(i) == 1) printf("%d\n", i);
    }
    return 0;
}

by MC_dream_tsr @ 2024-08-28 15:29:34

@初星逝者 谢谢,过了


by MC_dream_tsr @ 2024-08-28 15:30:11

@intqwe123 ok


by MC_dream_tsr @ 2024-08-28 15:31:47

@zhaoyonghao 好吧,谢谢


|