简体   繁体   中英

Why does my Python Code return an NZEC error

I'm trying to solve a problem on Sphere Online Judge(SPOJ) where it requires me to print all the integers between 1 and n that are divisible by x but not y, My code is right when I test on the Python IDE but when I try to input it on SPOJ, I get runtime error(NZEC), what is NZEC and why do I get it? This is my code:

test_cases = raw_input()
input = []
list = []
for z in xrange(int(test_cases)):
    input = raw_input()
    n,x,y = input.split(' ')
    for z in xrange(int(n)):
        if z%int(x) == 0 and z%int(y) != 0:
              list.append(z)
    answer1 = str(list).strip('[]')
    answer2 = answer1.replace(',', '')
    print answer2

Are you perhaps talking about this problem?

I see a few issues in your code:

  1. You are getting NZEC because perhaps there is an empty line in the input which you haven't taken into consideration. (See below for trivial modification to address this)

  2. Also, your code has a bug because z starts from 0 through n-1, whereas z should be > 1, so z should be in xrange(2,int(n))

  3. You don't need to strip, and then replace comma with space. You can do that in a single go (see below)

I have modified your code a little bit and it passes the test cases.

def get_line():
    while True:
        line = raw_input().rstrip()
        if not line:
            pass
        else:
            return line

test_cases = get_line()
for _ in xrange(int(test_cases)):
    input = get_line()
    n,x,y = [int(z) for z in input.split(' ')]
    list = []
    for z in xrange(2,n):
        if z%x == 0 and z%y != 0:
              list.append(z)
    answer1 = ' '.join(map(str,list))
    print answer1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM