IAM被陷在codechef SIGSEGV运行时错误,当我提出我的代码

问题描述:

这是我的代码IAM被陷在codechef SIGSEGV运行时错误,当我提出我的代码

#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运行时错误...请帮我写一个高效的程序 linkto问题是http://www.codechef.com/APRIL14/problems/ADIGIT

+1

你没有错误检查fopen的调用 - 你怎么知道文件被成功创建? –

+1

为什么当您打开文件进行写作时,评论会说“文件阅读”?误导评论比没有评论更糟糕。 – tripleee

您不允许在Codechef上打开读取/写入文件。这就是为什么你得到SIGSEGV。

我不明白需要为问题做文件I/O。

此外,您应该删除代码,因为这是运行竞赛的问题。