#18429: [python]try except用法容易造成TLE???


willson318 (willson)

學校 : 不指定學校
編號 : 81391
來源 : [163.24.49.56]
最後登入時間 :
2024-05-22 14:25:07

麻煩各位熟悉python的大大有空幫忙一下

用try except的寫法,在最後的一筆測資一直造成TLE

但改用第二種寫法,就AC了

第一種寫法:try except(最後一筆測資TLE)

#coding=UTF-8
import sys
Dict={"0 1 0 1":"A","0 1 1 1":"B","0 0 1 0":"C","1 1 0 1":"D","1 0 0 0":"E","1 1 0 0":"F"}

while True:
    try:
        N=input()       
        ans=""
        for i in range(int(N)):
            sIn=input()
            ans +=Dict[sIn]
            
            
        print (ans)  
    except EOFError:
        break
第二種寫法:AC
#coding=UTF-8
import sys
Dict={"0 1 0 1":"A","0 1 1 1":"B","0 0 1 0":"C","1 1 0 1":"D","1 0 0 0":"E","1 1 0 0":"F"}
#print (Dict["0 1 0 1"])
for i in sys.stdin:
    n = int(i)
    ans = ""
    for i in range(n):
        sIn=sys.stdin.readline()
        sIn=sIn.replace("\r","").replace("\n","")
        ans +=Dict[sIn]
    
    print (ans)
以上謝謝!!
#18430: Re:[python]try except用法容易造成TLE???


asnewchien@gmail.com (david)

學校 : 南投縣立旭光高級中學
編號 : 68108
來源 : [114.42.176.221]
最後登入時間 :
2025-10-04 22:52:03

 

input 不夠快吧。

#18432: Re:[python]try except用法容易造成TLE???


asnewchien@gmail.com (david)

學校 : 南投縣立旭光高級中學
編號 : 68108
來源 : [114.42.176.221]
最後登入時間 :
2025-10-04 22:52:03

站內的範例也是

import sys

for s in sys.stdin:

    print('hello, '+s)