首页 > 试题广场 >

数串

[编程题]数串
  • 热度指数:99998 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
设有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
while True:
    try:
        n=int(input().strip())
        #print(n)
        num_list=list(input().split(' '))
        #print(num_list)
        num_list1=num_list.copy()
        for i in range(n):#利用冒泡排序的思想
            for j in range(1,n-i):
                if int(num_list1[j-1]+num_list1[j])<int(num_list1[j]+num_list1[j-1]):
                    num_list1[j-1],num_list1[j]=num_list1[j],num_list1[j-1]
        result=''.join(num_list1)
        print(result)
    except:
        break

参考大佬的思想,利用冒泡排序的思想可以实现

发表于 2019-06-11 10:38:56 回复(0)
Python3 不用任何import的版本, 使用内置比较函数。
class Str(str):
    def __lt__(self, other):
        return str.__lt__(self + other, other + self)

while True:
    try:
        n = int(input())
        a = list(map(Str, input().rstrip("\n").split()))
        print(int("".join(sorted(a, reverse=True))))
    except:
        break


编辑于 2018-10-05 18:27:53 回复(0)
from functools import cmp_to_key def cmp(a, b):
    ab = int(a+b)
    ba = int(b+a)  return 1 if ab > ba else -1 
n = input() num = input() l=num.split(' ')
l.sort(key=cmp_to_key(cmp), reverse=True) print (int(''.join(l)))


发表于 2018-09-13 19:17:27 回复(0)

分享一个很好理解的冒泡算法,python3.5,因为我自己不会用cmp,所以就用了这个笨一点的办法

N = int(input())
nums = list(map(str,input().split()))
for i in range(len(nums)):
    for j in range(1,len(nums)-i):
        if nums[j-1]+nums[j] < nums[j]+nums[j-1]:
            nums[j-1],nums[j] = nums[j],nums[j-1]
res = int("".join(nums))
print(res)
编辑于 2018-09-05 19:10:27 回复(2)
from functools import cmp_to_key
n = input()
arr = sorted(input().split(), key=cmp_to_key(lambda x,y:int(x+y)-int(y+x)), reverse=True)
print("".join(arr))
看完讨论后自己的写法,集大家之所长,其实还是不懂cmp_to_key的运转方式
发表于 2018-08-02 15:09:01 回复(0)
原理:通过字符串相加拼接比较大小 自从用了python之后好久没有手写过排序了,于是乎强行不用sort手写一个插入排序用
import sys
def ncmp(x, y):
    if (x+y) < (y+x): return True
    else:return False

def insert_sort(lists):
    count = len(lists)
    for i in range(1, count):
        key = lists[i]
        j = i - 1
        while j >= 0:
            if ncmp(lists[j], key):
                lists[j+1] = lists[j]
                lists[j] = key
            j -= 1
    return lists
    
for i, v in enumerate(sys.stdin.readlines()):
    if i % 2 == 1:
        v = v.strip().split(' ')
        res = insert_sort(v)
        print("".join(v).lstrip("0") or "0")

发表于 2018-07-25 20:24:06 回复(0)
import sys  def compare(list_x,list_y):  return list_y + list_x > list_x + list_y  for k, a in enumerate(sys.stdin.readlines()):  if k % 2 == 1:
        a = a.strip().split()  for i in range(len(a)-1):  for j in range(len(a)-1-i):  if compare(a[j],a[j+1]):
                    a[j],a[j+1]=a[j+1],a[j]  print("".join(a))

编辑于 2018-04-15 22:33:28 回复(1)
#coding:utf-8

import sys

N=int(input())
numbers=sys.stdin.readline().strip().split()
numbers.sort(cmp=lambda x, y: cmp(x + y, y + x),reverse=True)#对相邻的两个数比较‘12123’和‘12312’的大小,cmp(a,b):a<b时返回-1,a=b时返回0,a>b时返回1
#sort排序,reverse=True保证排成的数字最大
ans=''.join(numbers)
print(ans)

发表于 2018-04-13 12:12:38 回复(0)
Python3
from functools import cmp_to_key num = int(input().strip()) l = input().strip().split(' ') print ("".join(sorted(l, key=cmp_to_key(lambda x,y: -1 if x+y > y+x else 1))))

编辑于 2018-03-30 09:17:29 回复(0)
def str_cmp(s1, s2):
    if s1 + s2 > s2 + s1:
        return 1
    elif s1 == s2 :
        return 0
    else:
      return -1
a = raw_input().strip().split()
a = [str(i) for i in a]
b = sorted(a, str_cmp ,reverse = True)
print ''.join(b)
发表于 2018-03-26 17:21:17 回复(0)
def mySort(n,m):
    nums=m.split()
    for i in range(len(nums)-1):    
        for j in range(len(nums)-i-1): 
            if nums[j]+nums[j+1]< nums[j+1]+nums[j]:
                nums[j], nums[j+1] = nums[j+1], nums[j]
    print (''.join(nums))
mySort(input(),input())

这在Python 中是一个排序问题,如果
str A; str B; if int(A+B)>int(B+A): int(A)>int(B)

发表于 2018-03-24 13:42:58 回复(0)
while True:
    try:
        n = int(raw_input().strip())
        l = raw_input().strip().split()
        print "".join(sorted(l,lambda x,y:-1 if x+y>y+x else 1))
    except:
        break

发表于 2018-01-26 14:16:03 回复(0)
targetinput = int(input())                        
numbers=list(input().split(' '))
result = ''
def findamax(numbers):
    global result
    if(len(numbers)<1):
        return
    first = numbers[0]
    for elements in numbers:
        if(first + elements < elements + first):
            first = elements
    result = result + first    
    numbers.remove(first)
    findamax(numbers)
    return result
print(findamax(numbers))
发表于 2018-01-24 15:51:47 回复(1)
"""
功能:数串
描述:将一组数排列成一排,组成一个最大的整数
作者:Kyle_deng
"""


nums_total = int(input()) #获得数字总个数
nums_list = input().split() #获得数字信息,字符格式

#重新排序
sorted_list = []
while nums_list :
    cur_num = nums_list[0]
    for num in nums_list :
        if (cur_num + num) < (num + cur_num) :
            cur_num = num
    sorted_list.append(cur_num)
    nums_list.remove(cur_num)

for num in sorted_list : #显示结果
    print(num, end='')

发表于 2017-12-26 22:38:26 回复(0)
#python3 没有cmp 真麻烦,幸好找到cmp_to_key
from functools import cmp_to_key
num = input()
l = input().split(' ')
l.sort(key=cmp_to_key(lambda a, b: (int(a + b) > int(b + a)) - (int(a + b) < int(b + a))), reverse=True) print(''.join(l))

发表于 2017-09-23 11:41:23 回复(0)