不知道哪里错了

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

ws778 @ 2024-12-09 21:35:38

#include<stdio.h>
#include<math.h>
int sum(int n);
int main()
{
    int n,m;
    scanf("%d%d",&m,&n);
    for(int i=m;i<=n;i++)
    {

        if(sum(i)==0)
        continue;
        else
        {
            int g=0;
            for(int j=2;j<i;j++)
            {
                if(i%j==0)
                g=1;
            }
            if(g==0)
            printf("%d\n",i);
        }
    }

}
int sum(int n)
{
    int len=log10(n)+1;
    int g;
    int h=n;
    int sum=0;
    while(len--)
    {
        g=n%10;
        n/=10;
        sum+=g*pow(10,len);
    }
    if(sum==h)
    return 1;
    else
    return 0;
}

by ckn0915 @ 2024-12-16 08:16:18

@ws778

#include<bits/stdc++.h>
using namespace std;
int c(int b){  
    int a=1;
    int smu=0,ans=0;
    for(int i=b;i>0;i=i/10) {ans=ans*10;ans=ans+i%10;}
    if(ans!=b) return 0;
    if(b==1) return 0;
    for(int i=2;i*i<=b;i++){
        if(b%i==0) return 0;
    }
    return 1;
}
int main(){
    int a,b;
    cin>>a>>b;
    for(int i=a;i<=b;i++){
        if(c(i)==1) cout<<i<<endl;
    }

    return 0;
}

by LiuSC_SZ @ 2024-12-17 20:37:08

用C++11,开启O2优化方可AC

#include <stdio.h>
#include <math.h>

int l,r;
bool check1(int x);
bool check2(int x);

int main() {
    scanf("%d %d", &l, &r);
    for(int i = l;i <= r;i++) {
        if(check1(i) == 0 || check2(i) == 0) {
            continue;
        }
        printf("%d\n",i);
    }   
    return 0;
}

bool check1(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 check2(int x) {
    if(x == 2) return 1;
    for(int i = 2;i <= sqrt(x);i++) {
        if(x % i == 0) return 0;
    }   
    return 1;
}

|