#26296: 分享 python 0.5s 的寫法


asnewchien@gmail.com (david)

學校 : 不指定學校
編號 : 68108
來源 : [114.42.154.168]
最後登入時間 :
2024-04-27 22:14:03
d190. 11462 - Age Sort -- UVa11462 | From: [1.168.19.178] | 發表日期 : 2021-07-31 15:37

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
d190. 11462 - Age Sort -- UVa11462 | From: [1.172.242.55] | 發表日期 : 2021-08-01 15:18

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.154.168]
最後登入時間 :
2024-04-27 22:14:03
d190. 11462 - Age Sort -- UVa11462 | From: [1.168.18.73] | 發表日期 : 2021-08-01 16:10

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

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

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


asnewchien@gmail.com (david)

學校 : 不指定學校
編號 : 68108
來源 : [114.42.154.168]
最後登入時間 :
2024-04-27 22:14:03
d190. 11462 - Age Sort -- UVa11462 | From: [111.246.62.234] | 發表日期 : 2021-08-02 10:54

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

 
ZeroJudge Forum