【C语言】向各位大佬求助 最后一个测试点超时了 不知道如何修改

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

mapple @ 2022-12-17 22:32:01

#include<stdio.h>
#include<math.h>
int zhishu(int m);
int main()
{
    int a,b,i,t,sum=0;
    scanf("%d%d",&a,&b);
    for(i=a;i<=b;i++)
    {
        sum=0;
        t=i;
        while(t!=0)
        {
            sum=sum*10+t%10;
            t=t/10;
        }
        if(sum==i)
        {
            if(zhishu(i)==1)
            {
                printf("%d\n",i);
            }
        }
    }   
    return 0;
} 
int zhishu(int m)
{
    int i;
    if(m==1)
    return 2;
    for(i=2;i<=sqrt(m);i++)
    {
        if(m%i==0)
        return 2;   
    }   
    return 1;
} 

by ChickenDrinkingMilk @ 2022-12-17 22:34:40

您可以用质数筛


by condfyll @ 2022-12-17 23:17:56

可能有点帮助


by mapple @ 2022-12-18 20:56:28

@HardsoftCyril ```c

```c
#include<stdio.h>
#include<math.h>
int zhishushai(int m);
int main()
{
    int a,b,i,t,sum=0;
    scanf("%d%d",&a,&b);
    int z[b+1],n,j;
    for(i=0;i<=b;i++)
    {
        z[i]=1;
    }
    z[1]=0;
    for(i=2;i<=b;i++)
    {
        if(z[i])
        {
            n=2;
            for(j=i;j*n<=b;n++)
            {
                z[j*n]=0;
            }
        }
    }
    for(i=a;i<=b;i++)
    {
        sum=0;
        t=i;
        while(t!=0)
        {
            sum=sum*10+t%10;
            t=t/10;
        }
        if(sum==i)
        {
            if(z[i]==1)
            {
                printf("%d\n",i);
            }
        }
    }   
    return 0;
} 

****使用质数筛内存超出是啥问题呢


|