66分救命!!!

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

midsummer_zyl @ 2023-07-09 17:14:14

大佬帮帮忙!

#include <bits/stdc++.h>
using namespace std;
int ss(int x) {
    if(x <= 1)
        return 0;
    if(x == 2)
        return 1;
    for (int i = 2; i < x; i++)
        if(x % i == 0) 
            return 0;
    return 1;
}
int main() {
    long long n, m, s = 0;
    scanf("%lld%lld", &n, &m);
    for (long long i = n; i <= m; i++) {
        long long j = i;
        s = 0;
        while (j != 0) {
            s = s * 10 + j % 10;
            j /= 10; 
        }
        if(s == i && ss(i) == 1)
            printf("%lld\n", i);
    }
    return 0;
}

by I_never_left @ 2023-07-09 17:38:44

@midsummer_zyl 把质数筛出来判回文,代码要的话等一下


by midsummer_zyl @ 2023-07-09 18:04:42

ok,谢谢!


by midsummer_zyl @ 2023-07-09 18:04:58

@hehe625


by midsummer_zyl @ 2023-07-09 18:08:22

可以互关吗?


by I_never_left @ 2023-07-09 18:10:27

@midsummer_zyl 啊看要求


by midsummer_zyl @ 2023-07-09 18:12:58


by midsummer_zyl @ 2023-07-09 18:38:00

@hehe625

写好了吗?


by I_never_left @ 2023-07-09 18:38:45

@midsummer_zyl 嗯,寄了,正调ing,脑子抽了一下


by I_never_left @ 2023-07-09 18:41:12

@midsummer_zyl 萌新代码最好自己写,以后才能学好,代码仅供参考

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ll;
const int N = 10000005;

int a, b;
int vis[N], p[1000010], cnt;
bool ss[N];

bool check(int x) {
    int u = x, v = 0;
    while(x) {
        v = v * 10 + x % 10;
        x /= 10;
    }
    return u == v ? 1 : 0;
}

int main() {
    scanf("%d%d", &a, &b);
    if(b>10000000) b=10000000;

    for(int i = 2; i <= b; ++ i) {
        if(!vis[i]) p[++ cnt] = i, ss[i] = 1;
        for(int j = 1; j <= cnt && i * p[j] <= b; ++ j) {
            vis[i * p[j]] = 1;
            if(i % p[j] == 0) break;
        }
    }

    for(int i = a; i <= b; ++ i) {
        if(!ss[i] || !check(i)) continue;
        printf("%d\n", i);
    }
    return 0;
}

by I_never_left @ 2023-07-09 18:42:30

@midsummer_zyl 记得看主页粉福,hh


| 下一页