c++ - Segmentation violation occurs when doing USACO's sprime
PHP中文网
PHP中文网 2017-07-06 10:35:02
0
1
4026
/*
ID: hywhuan1
LANG: C++
TASK: sprime
*/
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <sstream>
using namespace std;
const int maxn = 100000000;
int p[100000008], in, out;
int check(int n)
{
    string ss, st;
    stringstream stream;
    stream << n;
    stream >> ss;
    int len = ss.length();
    for (int i = len; len >= 1; len--)
    {
        st = ss.substr(0, len);
        stringstream stream(st);
        int stnum;
        stream >> stnum;
        if (p[stnum] == 0)
            return 0;
    }
    return 1;
}
ofstream fout("sprime.out");
ifstream fin("sprime.in");
int main()
{
    memset(p, 0, sizeof(p));
    p[2] = 1;
    for (int i = 3; i <= maxn; i += 2)
    {
        p[i] = 1;
    }

    for (int i = 3; i <= maxn; i++)
        if (p[i] == 1)
            for (int j = i + i; j <= maxn; j += i)
            {
                p[j] = 0;
            }

    fin >> in;
    for (int i = pow(10, in - 1); i < pow(10, in); i++)
    {
        if (check(i) == 1)
            fout << i << endl;
    }
    return 0;
}

I think it is a very simple idea of ​​​​making a table first and then making a judgment. It works fine on Win 10, but it does not work on USACO

  > Run 1: Execution error: Your program (`sprime') exited with signal
        #11 (segmentation violation [maybe caused by accessing memory out
        of bounds, array indexing out of bounds, using a bad pointer
        (failed open(), failed malloc), or going over the maximum
        specified memory limit]). The program ran for 0.000 CPU seconds
        before the signal. It used 4180 KB of memory. 

I really don’t understand the reason

Attached is the Chinese translation of this question: http://www.nocow.cn/index.php...

Welcome all experts to help me, a weakling, answer the question

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(1)
扔个三星炸死你

The memory has overflowed. The array you opened is too large.

It is estimated that USACO limits the memory size

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template