當我在Topcoder problem中運行我的代碼時,它顯示出現以下錯誤:IndexError:列表索引超出範圍。但是,代碼在Python IDLE上完美工作。誰能告訴我哪裏錯了?IndexError:列表索引超出Topcoder提交範圍
Problem Info:
Definition Class:
CheeseSlicing Method: totalArea
Parameters: integer, integer, integer, integer
Returns: integer Method
signature: def totalArea(self, A, B, C, S):
Examples
0) 1 3 3 2
Returns: 0
One of the dimensions of this block is 1. Regardless of how we cut it, each piece will have one dimension equal to 1. As S=2, this means that producing a good slice is impossible. Hence, the maximum total surface area of good slices is 0.
import sys
total=0
data=sys.stdin.read().split()
x,y,z,s=int(data[0]),int(data[1]),int(data[2]),int(data[3])
if min(x,y,z)==s:
print x*y*z/s
elif min(x,y,z)<s:
print 0
elif max(x,y,z)>s:
lines=[x,y,z]
while max(lines)>=2*s:
area=1
maxline=max(lines)
lines.pop(lines.index(maxline))
for line in lines:
area=area*line
total+=area
lines.append(maxline-s)
area=1
minline=min(lines)
lines.pop(lines.index(minline))
for line in lines:
area=area*line
total+=area
print total
我不知道,這個代碼如何在你的IDLE中完美工作。 split()函數將返回帶有一個項目的列表。 –
Topcoder有可能在此問題的驗證測試中遇到一些問題。 –
例如,輸入將是'5 5 5 2',split()函數將返回['5','5','5','2']。 –