我在練這些問題: http://code.google.com/codejam/contest/32016/dashboard#s=p1&a=1谷歌Codejam奶昔例子:找不到錯誤
我相信我有正確的解決方案,(即使在輸入和輸出的人工檢查); 我仍然收到Incorrect Output
任何人都可以指出我哪裏可能是錯的?
def process_file(file):
fsock = open(file)
text = fsock.read()
fsock.close()
lines = text.split('\n')
return lines
def process_lines(lines):
cur = 1
ans = []
while cur < len(lines) - 1:
N = int(lines[cur])
cur += 1
M = int(lines[cur])
cur += 1
struct = []
for i in range(0, M):
cust_pref = [int(n) for n in lines[cur].split(' ')]
cust_drinks = [a-1 for a in cust_pref[1::2]]
cust_drinks_malt_pref = cust_pref[2::2]
cust_choice = [(cust_drinks[i], cust_drinks_malt_pref[i]) for i in range(0, len(cust_drinks))]
cur += 1
struct.append(cust_choice)
ans.append((N, struct))
return ans
def process_case(case):
milkshake_menu = [0] * case[0] # our default menu
i = 0
impossible = False
while i < len(case[1]): # i represents the customer number, case[1] represents customers
acceptable = False
customer = case[1][i]
for drink_preferred in customer:
if milkshake_menu[drink_preferred[0]] == drink_preferred[1]:
acceptable = True
i += 1 # ok, next customer
break
if not acceptable:
for drink_preferred in customer:
# find a malted preference
if drink_preferred[1] == 1 and milkshake_menu[drink_preferred[0]] != 1:
# he needs a malted one
milkshake_menu[drink_preferred[0]] = 1
# but then we have to test previous customers, reset i
i = 0
break
#if you have come here, the customer does not have a malted preference, or has a unmalted preference that conflicts with other customer
impossible = True
#impossible is True, break outer loop
if impossible:
break
if impossible:
return "IMPOSSIBLE"
else:
return " ".join([str(n) for n in milkshake_menu])
if __name__ == "__main__":
import sys
filename = sys.argv[1]
lines = process_file(filename)
inp = process_lines(lines)
for k, v in enumerate(inp):
a = process_case(v)
print "Case #%d: %s" % (k + 1, a)
引擎收錄輸出:http://pastebin.com/uXJQKbSr
這並非如此工作。請閱讀[常見問題]和[問]。 – 2012-02-24 20:32:28
你有沒有試過它的樣本數據?它工作嗎? – katrielalex 2012-02-24 20:32:39
是的,它輸出一個有效的答案。我應該發佈嗎? – 2012-02-24 20:34:11