2017-05-06 71 views
0

當我在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 
+0

我不知道,這個代碼如何在你的IDLE中完美工作。 split()函數將返回帶有一個項目的列表。 –

+0

Topcoder有可能在此問題的驗證測試中遇到一些問題。 –

+0

例如,輸入將是'5 5 5 2',split()函數將返回['5','5','5','2']。 –

回答

0

你得到指數錯誤,因爲你的data是空的,由於在由TopCoder公司提供的測試輸入意外EOF。你可以嘗試實現你的代碼來處理輸入之間的EOF異常。

+0

你是什麼意思'實現你的代碼'? –

+0

您可以在輸入時實現異常處理 – Tarptaeya

相關問題