c++ - 如何解决非常大的数组输入的 SIGSEGV 错误?
问题描述
因此,我正在解决 HackerEarth 中的一个问题,该问题将您的代码测试到非常大的测试用例中。因此,当我尝试提交代码时,它通过了前 6 个测试用例,对于其他 5 个测试用例,它给出了“超出时间限制”,对于所有其他测试用例,它给出了 SIGSEGV 信号。
这是代码:
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long unsigned int tc,b, a , d,c;
cin>>tc;
for(int i=0;i<tc;++i)
{
cin>>a;
cin>>d;
cin>>c;
cin>>b;
long long unsigned int arr[b+1];
arr[0]=a;
arr[1]=d;
arr[2]=c;
if(b>=3){
for(long long unsigned i=3;i<=b;++i)
{
// arr[i]%=1000000007;
(arr[i])=(arr[i-1]+3*arr[i-3]+2*i)%1000000007;
arr[i]%=1000000007;
}
printf("%llu \n",arr[b]%1000000007);
}
else{
printf("%llu \n",arr[b]%1000000007);
}
}
return 0;
}
请帮忙。谢谢你。
解决方案
您正在尝试访问您无法访问的内存。这就是 SIGSEGV 的原因。您需要静态而不是动态地声明数组大小。
但是,最好使用向量,因为您使用的是 C++。
推荐阅读
- angular - Angular:如何根据键值更改值
- firebase - 谷歌登录 - 不能在生产中工作
- enterprise-architect - 以编程方式获取每个原型的链接对象的最佳方法
- javascript - 如果值在打字稿数组中,则分配新的字符串值
- python - Adding a UniqueConstraint to a ManyToMany field
- c++ - 输出浮点数时如何正确使用 %f 和 %lf?
- google-cloud-platform - 如何处理匿名调用者没有 storage.objects.get 访问 Google Cloud Storage 对象的权限
- html - 如何将父 div 缩小到其子级的大小?
- javascript - 在 JavaScript 中从单个数组创建 3 个不同的数组
- javascript - 动态 React 路径路由与 url 不匹配,但在重新加载时匹配