Python 2.7 AP2的spoj上python中的NZEC
我写了以下两个代码 FCTRL2.py 及 AP2.pyPython 2.7 AP2的spoj上python中的NZEC,python-2.7,return,return-value,Python 2.7,Return,Return Value,我写了以下两个代码 FCTRL2.py 及 AP2.py spoj接受FCTRL2.py,而AP2.py给出NZEC错误。这两种方法在我的机器上都能很好地工作,我没有发现它们在返回值方面有什么不同。请解释两者的区别以及如何避免AP2的NZEC错误。py第10行中的n可以是浮点,范围函数需要一个整数。因此,程序将以异常退出 我在Windows上测试了以下值: >ap2.py 23 4 7 9 1.6363636363636365 Traceback (most recent call las
spoj接受FCTRL2.py,而AP2.py给出NZEC错误。这两种方法在我的机器上都能很好地工作,我没有发现它们在返回值方面有什么不同。请解释两者的区别以及如何避免AP2的NZEC错误。py第10行中的n可以是浮点,范围函数需要一个整数。因此,程序将以异常退出 我在Windows上测试了以下值:
>ap2.py
23
4 7 9
1.6363636363636365
Traceback (most recent call last):
File "C:\martin\ap2.py", line 10, in <module>
for j in range(0,n):
TypeError: 'float' object cannot be interpreted as an integer
>ap2.py
23
4 7 9
1.6363636363636365
回溯(最近一次呼叫最后一次):
文件“C:\martin\ap2.py”,第10行,在
对于范围(0,n)内的j:
TypeError:“float”对象不能解释为整数
第10行中的n可以是浮点,范围函数需要一个整数。因此,程序将以异常退出
我在Windows上测试了以下值:
>ap2.py
23
4 7 9
1.6363636363636365
Traceback (most recent call last):
File "C:\martin\ap2.py", line 10, in <module>
for j in range(0,n):
TypeError: 'float' object cannot be interpreted as an integer
>ap2.py
23
4 7 9
1.6363636363636365
回溯(最近一次呼叫最后一次):
文件“C:\martin\ap2.py”,第10行,在
对于范围(0,n)内的j:
TypeError:“float”对象不能解释为整数
输入中可能有额外的空格。一个好的问题解决者会确保输入满足指定的格式。但是,由于spoj允许几乎任何人添加问题,这样的问题有时会出现。缓解空白问题的一种方法是立即读取输入,然后将其标记化
import sys; # Why use ';'? It's so non-pythonic.
inp = sys.stdin.read().split() # Take whitespaces as delimiter
t = int(inp[0])
readAt = 1
for i in range (0,t):
x,y,z = map(int,inp[readAt:readAt+3]) # Read the next three elements
n = (2*z)/(x+y)
d = (y-x)/(n-5)
a = x-(2*d)
print n
#for j in range(0,n):
# sys.stdout.write(a+j*d)
# sys.stdout.write(' ')
#print ' '
print ' '.join([str(a+ti*d) for ti in xrange(n)]) # More compact and faster
readAt += 3 # Increment the index from which to start the next read
输入中可能有额外的空格。一个好的问题解决者会确保输入满足指定的格式。但是,由于spoj允许几乎任何人添加问题,这样的问题有时会出现。缓解空白问题的一种方法是立即读取输入,然后将其标记化
import sys; # Why use ';'? It's so non-pythonic.
inp = sys.stdin.read().split() # Take whitespaces as delimiter
t = int(inp[0])
readAt = 1
for i in range (0,t):
x,y,z = map(int,inp[readAt:readAt+3]) # Read the next three elements
n = (2*z)/(x+y)
d = (y-x)/(n-5)
a = x-(2*d)
print n
#for j in range(0,n):
# sys.stdout.write(a+j*d)
# sys.stdout.write(' ')
#print ' '
print ' '.join([str(a+ti*d) for ti in xrange(n)]) # More compact and faster
readAt += 3 # Increment the index from which to start the next read
输入将始终确保n为整数。输入将始终确保n为整数。