CodeChef 中代码的运行时错误 (SIGSEGV)

Runtime Error(SIGSEGV) for a Code in CodeChef

本文关键字:SIGSEGV 运行时错误 代码 CodeChef      更新时间:2023-10-16

我是初学者,这是我在Code Chef上的第一个问题。我在机器上运行我的代码并且工作正常,但是在Code chef上提交后,我收到此错误运行时错误(SIGSEGV)。我是否为问题使用了太多内存,或者我给出的输入错误?

这是我的代码,

#include <iostream>
#include <stdio.h>
using namespace std;
#include <math.h>
#define D(a,b) fabs(a-b)
#define Q(x1,y1) (sqrt((x1)*(x1)+(y1)*(y1)))
#define min(a,b) (a>b?a:b)
#include <cstdlib>
int main()
{
    long int i,k,testno,cones;
    double N[1000],dmid,r,R,x1,y1,temp=0;
    cin >> testno;
    for(i=0;i<testno;i++){
        cin >> r >> R;
        dmid=D(r,R)/2;
        cin >> cones;
        for(k=0;k<3;k++){
            cin >> x1 >> y1;
            if(Q(x1,y1)>dmid){
                N[k]= R-Q(x1,y1);
            }
            else{
                N[k]=Q(x1,y1)-r;
            }
        }    
        for(k=0;k<cones-1;k++){
            temp=min(N[k],N[k+1]);
        }
        printf("%.3f",temp);
        cout << endl;
    }    
    return 0;
}

和问题的链接:http://www.codechef.com/problems/COURSE编辑:不敢相信我犯了这么荒谬的错误。无论如何,我已经编辑了代码,但仍然收到相同的错误。

你写了...

for(k=0;k<3;k++)

考虑到这一点,总会有 3 个锥体。但这是错误的。你把锥体作为输入。所以这条线一定是,

for(k=0;k<cones;k++)

通过此版本,您将摆脱运行时错误。但公平地说,您当前的解决方案与正确的解决方案相去甚远。您可以从中阅读此问题的社论 此链接以获得正确的想法。祝你好运。:)