简体   繁体   English

为什么我会为此代码获取SIGABRT? (SPOJ上的PRIME1)

[英]Why do I get SIGABRT for this code? ( PRIME1 on SPOJ )

I was trying to solve this question on SPOJ. 我试图在SPOJ上解决这个问题。

http://www.spoj.com/problems/PRIME1/ http://www.spoj.com/problems/PRIME1/

I get a SIGABRT or a SIGKILL. 我得到SIGABRT或SIGKILL。 What is going wrong? 怎么了? It works okay on my compiler ( CodeBlocks ). 它可以在我的编译器(CodeBlocks)上正常工作。

I am using the standard Sieve of Erastosthenes algorithm. 我正在使用标准的Erastosthenes算法筛网。

//finding if a number is prime or not
bool is_Prime(int num)
{
    if ( num <= 1)
        return false;

    int upper_limit = sqrt(num);

    for( int i = 2; i <= upper_limit ; ++i)
        if( num % i == 0)
            return false;

    return true;
}

bool is_prime_array [ 1000000000 ];

void sieve_of_erastosthenes( int MAX )
{
    is_prime_array[1] = false;

    for(int i=2; i <= MAX ; ++i )
        is_prime_array[i]=true;

    int upper_limit = sqrt(MAX);

    for(int i = 2 ; i <= upper_limit ; ++i)
        if ( is_Prime(i) )
            for ( int j = i*i ; j<=MAX ; j += i)
                is_prime_array[j]= false;
}

int main()
{


    int T, left, right;

    cin.sync_with_stdio(false);

    cin>>T;

    while ( T-- )
    {
        cin >> left >> right;

        sieve_of_erastosthenes( right );

        for(int i = left ; i <= right ; ++i )
            if(is_prime_array[i] == true )
                cout<<i<<endl;
    }
    return 0;
}

Probably because your is_prime_array takes 1GB of memory. 可能是因为您的is_prime_array占用了1GB的内存。

And, besides, it is not the Sieve of Eratosthenes. 而且,它不是Eratosthenes的筛子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM