我在做codechef的this practice problem。我已经solved this in C,并试图在Python2.7中做同样的事情。我得到了NZEC错误的codechef判断,这是“非零退出代码”。我不明白为什么会这样。这个程序在我的电脑上运行得很好。什么样的情况会导致这个问题?在
import sys
from itertools import islice
def p():
cases = int(sys.stdin.readline())
for case in xrange(cases):
height = int(sys.stdin.readline())
triangle = [map(int, i.split()) for i in islice(sys.stdin,height)]
prev_row = triangle[0]
for i in xrange(1, height):
cur_row = triangle[i]
cur_row[0] += prev_row[0]
cur_row[len(cur_row) - 1] += prev_row[len(prev_row) - 1]
for j in xrange(1, len(cur_row) - 1):
if(prev_row[j - 1] > prev_row[j]):
cur_row[j] += prev_row[j - 1]
else:
cur_row[j] += prev_row[j]
prev_row = cur_row
print max(prev_row)
p()
请查看Codechef常见问题解答:
http://www.codechef.com/wiki/faq#Why_do_I_get_an_NZEC
更改此行:
为此:
^{pr2}$从docs:
演示:
不要混合使用file对象作为迭代器,并在对象上调用
.readline()
。通过在} documentation :
sys.stdin
上使用islice()
,您将对象视为迭代器,在幕后调用file.next()
。从^{解决方案是不使用
.readline()
或不将file对象用作迭代器。在本例中,使用next(sys.stdin)
而不是sys.stdin.readline()
来一致地将对象用作迭代器。这在任何情况下都比使用.readline()
更有效:甚至:
^{pr2}$相关问题 更多 >
编程相关推荐