PAT 甲级真题 1038 Recover the Smallest Number (30分) python实现

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.

Input Specification:
Each input file contains one test case. Each case gives a positive integer N (≤10
的​4次方) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print the smallest number in one line. Notice that the first digit must not be zero.

Sample Input:
5 32 321 3214 0229 87
Sample Output:
22932132143287

具体思路

哎哟,我也没看啥贪心算法,我就类比了下,那个排序中的基数排序(就是先比第一位,再比第二位,怎么怎么样,大致这个样子,如果说错了,请指出),每个数来比较用插入排序确定好序列,其中的比较,不再是比较大小,而是一个函数实现,思路讲的乱七八糟的,看代码

具体代码

def BIJIAO(A, B):#比较函数
    len1 = len(A)
    k = 0
    if len(A) > len(B):#len1为A,B短的那个长度
        len1 = len(B)
        k = 1
    for i in range(len1):
        if int(A[i]) < int(B[i]):#按位比较!
            return 1
        elif int(A[i]) > int(B[i]):
            return 0
    if k == 1:#如果比较完,前面的位数都一样,进行下一步比较
        for i in range(min(len(A) - len(B), len(B))):#我解释下,我这个思路  321 32这两个数,前两位是一样的
            if int(A[len(B) + i]) > int(B[i]):#,但是 32132<32321 的,所以进行循环比较,长的那个数接着向后比,短的从头来比较
                return 0
            elif int(A[len(B) + i]) < int(B[i]):
                return 1
    else:
        for i in range(min(len(B) - len(A), len(A))):
            if int(B[len(A) + i]) > int(A[i]):
                return 1
            elif int(B[len(A) + i]) < int(A[i]):
                return 0
    return k#最后如果还没有返回,就是 32 3232 这种数时,我们返回k
def mian():
    j = 0
    y = input('').split()#输入
    if len(y) == 1:
        return
    s = int(y[0])
    x = y[1:+1:]#x接受后面的数据
    x.sort()#要排一下序的,有点取巧了,不排就会超时。因为插入排序比较适合那种已经基本有序的数列,所以要排一下
    if len(x)==1:#只有一个数,就直接输出了
        print(int(x[0]))
        return
    for i in range(1, len(x)):#插入排序
        sum = x[i]
        j = i
        while j>0 and BIJIAO(sum,x[j-1]):#用了函数来作为是否需要移位的判断
            x[j] = x[j-1]
            j=j-1
        x[j] = sum
    qw = 0#首先,我这个排序函数,排完后啊,0肯定在最前面,所以我们要找到第一个非 0 元
    while qw<len(x) and int(x[qw])==0 :
        qw=qw+1
    if qw==len(x):#没找到,就输出 0了
        print("0")
        return
    print(int(x[qw]),end='')#第一个要用int形式输出哦
    for i in range(len(x)):
        if i!=qw:
            print(x[i],end='')#后面的不能用哦
    return 
mian()



具体结果

在这里插入图片描述
首先,我这个写的不好,中途思维跑偏写了很久,还是用的python,所以哪里不对,请指出 ( 第二个测试点 2 000 0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值