當前位置:首頁 » 編程語言 » python計算組合數

python計算組合數

發布時間: 2023-07-11 18:01:56

1. python實現,輸入一個正整數數組,把數組里所有數字拼接起來排成一個數,列印能拼接

你的例子第一列全是 3,我給個例子吧:[321, 32, 3, 4],輸出該是 321,32,3,4。

第一個數越大,則應該排在後面,畢竟 4XXX 是比 3XXX 大的。

setp1:[0][1][2]
321
32
3
4
排序第0列,越大的排越後。
ret=[?,?,?,4]

setp2:[0][1][2]
321
32
3<3><-補位3,因為3是同3組第一個元素。
排序第1列,越大的排越後。
ret=[?,?,3,4]

setp3:[0][1][2]
321
32<3><-補位3,因為3是同3組第一個元素。
排序第2列,越大的排越後。323比321大,所以……
ret=[?,32,3,4]

只剩一個,那個排第一:
ret=[321,32,3,4]

以上就是基本思路了。綜上可得:

1. 先按 [0] 列分組:

2. 組中每個數都補位到同樣長度,然後再排序。

完整代碼:

defjoinmin(ls):
groups={}
foriteminls:
prefix=item
n=0
whileprefix>10:
prefix//=10
n+=1
groups.setdefault(prefix,[]).append([item,n])
sorted_keys=list(sorted(groups))
ret=0
forprefixinsorted_keys:
items=groups[prefix]
max_n=max([t[1]fortinitems])
presort_items=[]
foritem,item_ninitems:
padding=item
n=item_n
whilemax_n>n:
padding*=10
padding+=prefix
n+=1
presort_items.append((padding,item,item_n))
for_,item,ninsorted(presort_items):
whilen>-1:
ret*=10
n-=1
ret+=item
returnret

不是看在你的分上答的,不過這種小題目蠻有趣的。

熱點內容
scratch少兒編程課程 發布:2025-04-16 17:11:44 瀏覽:639
榮耀x10從哪裡設置密碼 發布:2025-04-16 17:11:43 瀏覽:368
java從入門到精通視頻 發布:2025-04-16 17:11:43 瀏覽:84
php微信介面教程 發布:2025-04-16 17:07:30 瀏覽:310
android實現陰影 發布:2025-04-16 16:50:08 瀏覽:793
粉筆直播課緩存 發布:2025-04-16 16:31:21 瀏覽:344
機頂盒都有什麼配置 發布:2025-04-16 16:24:37 瀏覽:212
編寫手游反編譯都需要學習什麼 發布:2025-04-16 16:19:36 瀏覽:812
proteus編譯文件位置 發布:2025-04-16 16:18:44 瀏覽:366
土壓縮的本質 發布:2025-04-16 16:13:21 瀏覽:592