【问题标题】:IAM getting stuck with SIGSEGV runtime error in codechef when i submit my code当我提交我的代码时,IAM 在 codechef 中遇到 SIGSEGV 运行时错误
【发布时间】:2014-04-06 06:55:42
【问题描述】:

这是我的代码

#include<stdio.h>
long int a[100001],b[100001];
int main()
{
long int n=0,m=0,bp=0,bn=0,i=0,x=0,Result=0;//declaration
char ch='\0';
FILE *fp;
scanf("%ld %ld",&n,&m);
fp=fopen("array.txt","w");//file reading
for(i=0;i<=n;i++)
{
    ch=getchar();
    fputc(ch,fp);
}
fclose(fp);
fp=fopen("array.txt","r");//file scaning to array
for(i=0;i<n;i++)
{
    fscanf(fp,"%1d",&a[i]);
}
fclose(fp);
while(m>0)
{
    scanf("%ld",&x);
    bp=0;
    bn=0;
    for(i=0;i<x-1;i++)
    {
        b[i]=a[x-1]-a[i];
        if(b[i]>0)
            bp = bp+b[i];
        else
            bn = bn+b[i];
    }
    Result=bp-bn;
    printf("%ld\n",Result);
    m--;
}
return (0);
}

当我在我的系统中运行此代码时,它会给出正确的输出,但在 codechef 中却显示 SIGSEGV 运行时错误...请帮助我编写一个高效的程序 链接问题是http://www.codechef.com/APRIL14/problems/ADIGIT

【问题讨论】:

  • 您对 fopen 的调用没有错误检查 - 您怎么知道文件已成功创建?
  • 为什么在打开文件写入的时候注释说“正在读取文件”?误导性的 cmets 比没有 cmets 更糟糕。

标签: c segmentation-fault


【解决方案1】:

您不能在 Codechef 上打开文件进行读/写。这就是您获得 SIGSEGV 的原因。

我不明白是否需要针对该问题执行文件 I/O。

另外,你应该删除代码,因为这是运行比赛的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    相关资源
    最近更新 更多