问题
I have done a few questions in SPOJ using Python. I got a few correct. Rest all I keep on getting NZEC most of the time. I added sys.exit(0) at the end of the code. But still it shows the same.
I wanted to know if my some writing practice is making that error. Please suggest any changes. I think my algo is write but plz suggest any changes if its wrong.
Here is the link to the question.
http://www.spoj.com/problems/NSTEPS/
def check_num(m,n):
if(m!=n and m!=n+2):
return 0;
elif(m==n) :
if(m%2==1):
a=int((m+1)/2)-1;
return (1+a*4);
else :
a=int(m*2);
return(a);
elif(m==n+2):
if(n%2==1):
a=int((n+1)/2)+1;
return (3+a*4);
else :
a=int(n*2);
return(2+a);
import sys;
inp=sys.stdin.read().split("\n");
N=int(inp[0]);
i=1;
l=dict();
b=dict();
c=dict();
for i in range(1,N+1):
a=inp[i];
k=0;
b[i],c[i]=[int(a[k]),int(a[k+2])];
if (b[i]==0 and c[i]==0):
l[i]=0;
else :
l[i]=check_num(b[i],c[i]);
for i in range(1,N+1):
if (l[i]==0):
if (c[i]==0 and b[i]==0):
print l[i];
else:
print "No Number";
else :
print l[i];
sys.exit(0)
Thanks in advance.
回答1:
Basically you get nzec error or runtime error when your trying to access the array greater then its size or for some testcases your program may be running infinitely...
At the last , their might be some logical error in your code , try extreme test case in your code .
Since in spoj , the site is not maintained properly, for some question i tried i got tle in python and java but got AC in C and C++. This happen bcz the setter of the problem has not set the correct timing. This type of things have happen a lot to me .
Happy coding
回答2:
NZEC is mostly due to array indices. Eg: A general example: string s = ''; s[0] = 'a' will give NZEC Also array indices out of bounds will give NZEC
You should try to avoid array indices as far as possible. Instead use constructs like 'for..in'
回答3:
Try to replace your tabulation to spaces. Also big output can cause the problem, so try use sys.stdout.write() and sys.stdout.flush(). Another often causes: reaching boundaries of list, int('423\n') or other function(argument) calling when argument is none or in bad format, calling sys.stdin.readline() after reading all input would give not handled exception, other not handled exceptions.
来源:https://stackoverflow.com/questions/19876845/my-every-python-code-gives-nzec-in-spoj