moksha_ 2019-09-27 08:17 采纳率: 0%
浏览 320
已采纳

c语言寻找素数数据溢出问题

进行统计不同区间寻找素数的时间,当以50w为起点的时候显示溢出,之前都是正常运行

#include
#include
#include
#define Max 5000
#define Min 500

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int a[Max] = {0};
int i=0;
int j=0;
int count=0;

//基数位置赋予数字
for(i=Min;i<=Max;i++) 
{
    if(i%2!=0)
    {
        a[i]=i;
    }
}

//2是偶数是个例
a[2]=2;

//剩下的基数,基数倍清零
for(i=Min;i<=Max;i++)
{
    if(a[i]!=0)
    {
        j=i+2;
        while(j<=Max)
        {
            if(a[j]%a[i]==0)
            {
                a[j]=0;
            }
            j+=2;
        }
    }
}

//打印素数,也就是非零数字 
for(i=Min;i<=Max;i++)
{
    if(a[i]!=0)
    {
        printf("%d\t",a[i]);
        count++;
        if(count==4)
        {
            printf("\n");
            count=0;
        }
    }
}
return 0;

}

  • 写回答

2条回答 默认 最新

  • threenewbee 2019-09-27 09:40
    关注

    int a[Max] = {0};
    不要定义在堆栈上,可以用全局变量或者maloc动态分配
    堆栈默认只有2MB
    因此50万x4字节=2MB,再大就放不下了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?