这是我的代码
#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
答案 0 :(得分:2)
您不能在Codechef上打开文件进行读/写。这就是你获得SIGSEGV的原因。
我不明白是否需要为此问题执行文件I / O.
此外,您应该删除代码,因为这是运行竞赛时遇到的问题。