【问题标题】:Why is this code giving me segmentation, ? when we print only count then it gives answer and when print ans[4] or any value then it gives error?为什么这段代码给我分段?当我们只打印计数时它会给出答案,当打印 ans[4] 或任何值时它会给出错误?
【发布时间】:2021-08-04 19:20:57
【问题描述】:
I am getting segmentation error , it gives output when only print ans[k] inside the function then it 
   gives corrct output but in main when I try to print ans[k] then it gives segmentation 
i am new at progamming so i don't know more about it so please help me it's my assigment 

问题是

我必须创建一个最大为 4 * 10^6 的因子数组

Core Dump/Segmentation fault 是一种由访问“不属于您”的内存引起的特定错误。

当一段代码尝试在内存中的只读位置或已释放的内存块中执行读写操作时,称为核心转储。 这是表示内存损坏的错误。

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long int
    vector<int> arr(1000001,0);
    vector<int> ans;
    
    bool isPrime(int n)
    {
    
        if (n <= 1)
            return false;
        if (n <= 3)
            return true;
    
        if (n % 2 == 0 || n % 3 == 0)
            return false;
    
        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
    
        return true;
    }
    
     
    
    void factorpush()
    {
    
        for (int k = 5; k <= 4000001; k += 4)
        {
            if (isPrime(k))
            {
                arr[k] = 1;
            }
        }
        
        arr.clear();
        ans.clear();
        for (int k = 5; k <= 4000001; k += 4)
        {
            if (arr[k] == 1)
            {
                int n = (k / 4);
    
                ans[n] = (2 * n) - 1 + k;
                 
            }
    
            else
            {
                vector<int> v;
    
                for (int i = 1; i*i < k; i++)
                {
                    if (k % i == 0)
                    {
    
                        if (k / i == i && i != k)
                            v.push_back(i);
    
                        else
                      {
                        if (i != k)
                        {
                            v.push_back(i);
                        }
    
                        if (k / i != k)
                        {
                            v.push_back(k/ i);
                        }
                      }
                    }
                }
    
                int count = k;
                int count2 = 0;
                for (auto x : v)
                {
                    if (x != 1 && x!=k)
                    {
                        int n = (k/4);
                        count2 += ((2*n - 1)/x);
                        count +=  ((2*n - 1)/x)*x;
                    }
                    
                }
                
                 int n1 = (k/4);
                  int val = (2*n1) - 1 - count2;
                    
                  count += val;
                  
                   ans[n1] = count;
                 //cout<<ans[n1]<<"\n"; 
                  
            }
        }
    }
    
    int32_t main()
    {
        factorpush();
        int n;
        cin>>n;
        
        cout<<ans[n]<<"\n";` 
                  return 0;
    }

【问题讨论】:

  • 警告:#include &lt;bits/stdc++.h&gt;loads the gunusing namespace std;takes off the safety。非常非常小心。顺便说一句,您节省键入 #include &lt;bits/stdc++.h&gt; 而不是所需的包含的时间很快被构建过程消耗了大约十倍的时间。这种节省时间的操作通常会在编译两三个之后花费您的时间。

标签: c++ compiler-errors runtime-error dsa


【解决方案1】:

我没有好好阅读你的代码,可能还有其他错误,但至少结合了

    vector<int> arr(1000001,0);

        for (int k = 5; k <= 4000001; k += 4)
        {
            if (isPrime(k))
            {
                arr[k] = 1;
            }
        }

很糟糕,因为arr[k] 会比分配的更远。

你必须分配足够的元素,比如

    vector<int> arr(4000002,0);

arr.clear();ans.clear(); 之后访问arr[k]ans[n] 并且不添加任何元素是不好的,因为clear() 函数会删除向量中的所有元素。在擦除后还检查arr[k] == 1 没有意义。不太了解你的代码,看起来像

        arr.clear();
        ans.clear();

应该换成类似的东西

        ans.clear();
        ans.resize(1000001);

【讨论】:

  • 同样,据我所知,代码根本没有正确地将值放入ans,它只是立即开始写入ans[n]
  • @NathanPierson 是的,factorpush() 在打印ans[n] 之前被调用,但是原代码中的函数并没有给ans 添加任何元素。
猜你喜欢
  • 2018-05-13
  • 2019-10-05
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多