88分,最后一个测试点内存超了(MLE),求教

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

timeset0 @ 2024-10-09 13:16:50

#include <stdio.h>

#include <math.h>

int huiwen(int a)
{

    int n=a,i=0,j=0,x=0;
    int num[10];
    while (n!=0)
    {
        num[i]=n%10;
        n/=10;
        i++; j++; 
    }
    for (x=0;x<=j-1;x++)
    {
        if (num[0+x]!=num[j-1-x]) return 0;
    }
    return a;
}

int main(){

    int a,b,j,i=0,out;
    scanf("%d%d",&a,&b);
    int num[b+1];
    for (i=0;i<=b;i++)
    {
        num[i]=1;
    }
    i=2; num[0]=num[1]=0;
    for (i=2;i<=sqrt(b);i++)
    {
        if(num[i]) {
            for (j=i*i;j<=b;j+=i){
                num[j]=0;
            }
        }
    }
    i=a;
     for (i=a;i<=b;i++)
    {
        if(num[i]) {
            out=huiwen(i);
            if (out!=0) printf("%d\n",out);
        }
    }

    return 0;
}

by movefast @ 2024-10-09 13:21:01

你可以先看看数据范围


|