数串
题目描述
设有n个正整数,将他们连接成一排,组成一个最大的多位整数。
如:n=3时,3个整数13,312,343,连成的最大整数为34331213。
如:n=4时,4个整数7,13,4,246连接成的最大整数为7424613。
输入描述:
有多组测试样例,每组测试样例包含两行,第一行为一个整数N(N<=100),第二行包含N个数(每个数不超过1000,空格分开)。
输出描述:
每组数据输出一个表示最大的整数。
示例1
输入:
2
12 123
4
7 13 4 246
输出:
12312
7424613
Mycode:
import re
from functools import cmp_to_key
key = cmp_to_key(lambda x,y: int(y+x)-int(x+y))
lst = []
while True:
try:
a = int(input())
str = input().strip()
lst = str.split(" ")
lst.sort(key = key,reverse = False)
lst2 = "".join(lst)
print(lst2)
except:
break
Attention:
1.关于sorted cmp中的一些解释 |
---|
pytthon3已经取消了sorted和sort中的cmp,在手册中可以查到使用 |
from functools import cmp_to_key key = cmp_to_key(lambda x,y: int(y+x)-int(x+y)) |
2.sort和sorted的区别 |
L = [(‘b’,3),(‘a’,2),(‘c’,1),(‘d’,4)] |
print (sorted(L, key = lambda x: x[1] )) |
等价于print (L.sort(key = lambda x: x[1] )) |