#41697: python 不使用大量if的解法


sam851015@gmail.com (多挖鼻孔有益身心健康)

學校 : 臺中市立惠文高級中學
編號 : 277705
來源 : [123.192.228.253]
最後登入時間 :
2025-09-21 22:24:46

用一大堆if的可讀性太差了,寫完後很難debug

可以試試把資料都打包到一個list裡面

例如這樣(gist連結)

 
while True:
    # 無限循環至EOF為止
    try:
        a, b, c = map(int, input().split())
    except EOFError:
        break

    # 將候選人資料做成嵌套list, 然後讓程式排序他們
    candidate = [(a,'A'), (b,'B'), (c,'C')]
    candidate.sort()

    # 邏輯判斷
    if candidate[0][0] + candidate[1][0] > candidate[2][0]:
        print(candidate[1][1])
    else:
        print(candidate[2][1])