帮帮我这个蒻稽吧

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

wjm2012 @ 2024-09-30 12:30:37

#include<bits/stdc++.h>
using namespace  std;
int  main(){
    int a,b,d=1;
    long long ans[100]={0};
    cin>>a>>b;
    for(int i=a;i<=b;i++){
        int c=0;
        for(int k=2;k<9;k++){
            if(i%k==0){
                c++;
            }
        }
        if(c==0||i==2||i==3||i==5||i==7){
            ans[d]=i;
            d++;
        }
    }

    for(int i=1;i<d;i++){
        if(ans[i]!=0)
            cout<<ans[i]<<endl;
    } 
return 0;
}

这里我可以算出质数但不知道回文数怎么搞,帮帮我这个蒻稽吧,回帖必关


by csxx601cjy @ 2024-09-30 12:32:03

#include<bits/stdc++.h>
using namespace std;
int l, r;
bool check1(int x){
    if((1000 <= x && x <= 9999) || (100000 <= x && x <= 999999)) return 0;
    return 1;
} 
bool check2(int x){
    int a[20], flag = 1;
    while (x > 0){
        a[flag] = x % 10;
        x /= 10;
        flag++;
    } 
    for (int i = 1; i <= flag / 2; i++)
        if(a[i] != a[flag-i]) return 0;
    return 1;
} 
bool check3(int x){
    if(x == 2) return 1;
    for(int i = 2; i <= sqrt(x); i++)
        if(x % i == 0) return 0;
    return 1;
}
int main(){
    scanf("%d %d", &l, &r);
    if(l == 2) printf("2\n");
    if(l % 2 == 0) l++; 
    r = min(9999999, r);
    for(int i = l; i <= r; i = i + 2){
        if(check1(i) == 0) continue;
        if(check2(i) == 0) continue;
        if(check3(i) == 0) continue;
        printf("%d\n", i); 
    }   
    return 0;
}

|