#26296: 分享 python 0.5s 的寫法


asnewchien@gmail.com (david)

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

https://youtu.be/ZmP9N8lhKS8

#26312: Re:分享 python 0.5s 的寫法


406490150@gms.tku.edu.tw (我是朱朱)

學校 : 國立交通大學
編號 : 139794
來源 : [140.113.236.122]
最後登入時間 :
2022-09-03 11:13:16

https://youtu.be/ZmP9N8lhKS8

首先,先謝謝你的分享!

我一開始是用itertools.repeat以及' '.join(),速度耗時0.7秒,改用chain.from_iterable反而更慢。

沒想到字串相加、相乘會比套件快,真是神奇!還有字串+=的做法一直以為會很慢,沒想到比' '.join快!

我後來參考你的  字串相加相乘  做法,改寫輸出為stdout.write:

from sys import stdinstdout
from collections import Counter

for _ in stdin:
    cnt = Counter(stdin.readline().split())
    for k in sorted(cntkey=int):
        stdout.write((k + ' ') * cnt[k])
    stdout.write('\n')
 
#26314: Re:分享 python 0.5s 的寫法


asnewchien@gmail.com (david)

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

感覺字串的加法, 乘法及 count 都很好用。

不過這是出題後, 參考了很多人的寫法後的心得。

#26325: Re:分享 python 0.5s 的寫法


asnewchien@gmail.com (david)

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

解題報告不可以直接貼程式碼。