在下面的程序中用c++获取SIGABRT
Getting SIGABRT in c++ in the following program
我在spoj中提交了这个问题,这显示了runtime error(SIGABRT)
。它在我的机器和Ideone.com上运行正常,但显示错误。有什么原因吗?我在这里写代码:我想做的是计算10^8阶素数,并对其进行一些运算。以下是问题链接:http://www.spoj.com/problems/CPRIME
#include<iostream>
#include<vector>
#include<cmath>
#include<cstdio>
using namespace std;
int n=100000001;
int d = 10001;//3163;
vector<int>num(100000001,0);
int main (){
//atkin's seive for generating prime numbers of order 10^8
for (int x = 1; x <= d; x++){
for (int y = 1; y <= d; y++){
long long sqx=x*x,sqy=y*y;
long long m =(3*sqx)-sqy;
//m=m-sqx;
if(m<n){
if(x>y&&m%12==11)num[m]=num[m]^1;
m=m+2*sqy;
if(m<n){
if(m%12==7)num[m]=num[m]^1;
m=m+sqx;
if(m<n&&(m%12==1||m%12==5))num[m]=num[m]^1;
}
}
}
}
for (int i=5;i<d;i++){
if(num[i]){
for(int j=i*i;j<n;j+=i*i)num[j]=0;
}
} //sieve finished here
//main code start from here and for loop is for counting number of primes
//less than or equal to that number.
int add=2;
num[2]=1;num[3]=2;
for(int a=4;a<n;a++){
if(num[a]){
//if(a==2||a==3||a==5)cout<<num[a]<<" "<<a<<" ";
add++;
num[a]=add;
}
else num[a]=add;
}
//cout<<num[2]<<" "<<num[3]<<endl;
while(1){
int t;
scanf("%d",&t);
if(t==0)break;
double ans=(num[t]-t*1.0/(log(t)))*100.0/num[t];
if(ans<0)ans*=-1.0;
printf("%.1lfn",ans);
}
return 0;
}
您应该从堆而不是堆栈中将num分配为数组int* num=new int[size];
这会很好用。
100000001*4 = 400000004 bytes
400000.004 kilo bytes
400.000004 mega bytes
你至少需要半个演出因为矢量也增加了一些开销。
操作系统将运行bannanas,试图将其放入堆中。
顺便说一句:用O(n^2)时间计算素数是最天真、最慢的方法。考虑使用阿特金斯筛。
确保你没有被零整除。100.0/num[t]
。通常除以零生成SIGFPE
,但硬件也可以在这样的除法上生成SIGABRT
。
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 无法获取菜单选择以运行函数.C++
- 数组长度,为什么从命令行获取时不能使用它?
- Boost Spirit,获取迭代器内部语义动作
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 具有默认值的引用获取函数
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 在c++中获取大文件的大小