2011-12-29 60 views
-1

問題描述是在這裏:http://www.spoj.pl/problems/FASHION/SPOJ時裝給運行時錯誤的Python

過程:注意到兩個列表作爲輸入,使用Python中sort()方法對它們進行排序,然後打印總和

代碼:

import sys,os 
#Need to maximize the product of two lists 
def process(index,line): 
    p=line.split(" ") 
    #print 'Line after splitting',p 
    for i in p: 
     if(index==0): 
      men.append(int(i)) 
     else: 
      women.append(int(i)) 

global men 
global women 
men=[] 
women=[]  
''' First, you enter number of times you want to compare . 
    Second, you enter number of men/women 
    Then, you enter the real data 
''' 


n=int(raw_input()) #This is for number of shows 
num = int(raw_input()) #This is number of men/women 


for t in range(0,n): #Do this "n" times 

    men = [] 
    women = [] 
    for i in range(0,2): #Now, enter the men data first and women next 

     line=raw_input() 
     process(i,line) 
    p=0 

    temp = [] 
    men.sort() 
    women.sort() 

    for i in range(0,num): 
     p = p + men[i] * women[i] 
    print p 

問題:它不斷給運行時錯誤:(

我所遇到的一些情況:

In [16]: %run /home/crazyabtliv/SPOJ/Fashion.py 
2 
3 
1 1 1 
2 3 4 
9 
4 5 6 
0 9 8 
94 

In [14]: %run /home/crazyabtliv/SPOJ/Fashion.py 
1 
5 
1 1 0 0 0 
10 10 9 9 9 
20 

謝謝!

+0

你的[運行時錯誤](http://hs.spoj.pl/embed/guide)的代碼是什麼? – joaquin 2011-12-29 07:41:18

+0

爲什麼不嘗試用[測試輸入SPOJ在問題頁面中提供您?](http://www.spoj.pl/problems/FASHION/)來測試您的代碼。你有相同的輸出嗎? – joaquin 2011-12-29 07:57:43

回答

2

這就是SPOJ顯示它預計:

Input: 
2 
2 
1 1 
3 2 
3 
2 3 2 
1 3 2 

Output: 
5 
15 

但是,嘗試運行您的程序產生:

2 
2 
1 1 
3 2 
5  <- that is the output for the first contest. should not output yet 
3 
2 3 2 
Traceback (most recent call last): 
    File "dasdsad.py", line 35, in <module> 
    p = p + men[i] * women[i] 
IndexError: list index out of range 

C:\Python26\programas\zz_so> 

所以,你必須修改代碼,讓您的結果在內存中直到該最後輸入被輸入。那麼你也應該移動一些代碼,以便考慮到num在比賽中可能會有所不同。

這已被修改,以按預期方式工作:

def process(index,line): 
    p = line.split(" ") 
    for i in p: 
     if(index==0): 
      men.append(int(i)) 
     else: 
      women.append(int(i)) 

n = int(raw_input()) #This is for number of shows 
results = [] 
for t in range(0, n): #Do this "n" times 
    men = [] 
    women = [] 
    num = int(raw_input()) #This is number of men/women 
    for i in range(0,2): #Now, enter the men data first and women next 
     line=raw_input() 
     process(i,line) 

    p=0 
    men.sort() 
    women.sort() 

    for i in range(0,num): 
     p = p + men[i] * women[i] 
    results.append(p) 

for item in results: 
    print item 

靜止的代碼可以大大簡化:

def process(line): 
    return sorted([int(i) for i in line.split()]) 

n = int(raw_input())   #This is for number of shows 
results = [] 
for t in range(n):   #Do this "n" times 
    num = int(raw_input()) #This is number of men/women 

    men = process(raw_input()) 
    women = process(raw_input()) 

    p = sum(m*w for m, w in zip(men, women)) 
    results.append(p) 

for item in results: 
    print item 

編輯:我優化的比特與總和的代碼(generator_expresion )而不是for循環

+0

謝謝!對於這兩件事。修改後的代碼教會了我很多:)從下次開始寫得更好!再次感謝 – crazyaboutliv 2011-12-29 09:04:12

+0

歡迎您。如果你仔細觀察代碼,你可以做更多的事情。例如,你可以改變'p = 0 我爲範圍(num): p = p + men [i] * women [i]'with'p = sum([m * w for m,w in zip男人,女人)])' – joaquin 2011-12-29 09:26:10

+0

@crazyaboutliv最後一件事:如果你正在編碼spoj,並希望分析和改善你的運行代碼與其他人的意見,可能你想發佈你的建議在[CodeReview](http://codereview.stackexchange .com) – joaquin 2011-12-29 09:33:15