题解 | #字符串合并处理#

字符串合并处理

https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f


input = ''.join(input().split(' '))

ji = []
ou = []
for index in range(len(input)):
    if index%2==0:
        ou.append(ord(input[index]))
    else:
        ji.append(ord(input[index]))

ji_sorted = sorted(ji)
ou_sorted = sorted(ou)
new_str = []
for index in range(len(input)):
    if index%2==0:
        new_str.append(chr(ou_sorted.pop(0)))
    else:
        new_str.append(chr(ji_sorted.pop(0)))

res = []
for item in new_str:
    if item.isdigit():
        # 这里的重点16进制用二进制表示最大要用四位,转换二进制时,不足四位的要补足四位
        res.append(hex(int(bin(int(item))[2:].rjust(4,'0')[::-1],base=2))[2:].upper())
    elif item.isalpha() and item.lower() in ['a','b','c','d','e','f']:
        res.append(hex(int(bin(int(item,base=16))[2:].rjust(4,'0')[::-1],base=2))[2:].upper())
    else:
        res.append(item)
print(''.join(res))

#字符串合并处理#
全部评论

相关推荐

06-04 18:37
门头沟学院 Java
勇敢的ssr求对象:前面看的有点奔溃,看到只有你是真玩啊,忍不住笑出了声😂
点赞 评论 收藏
分享
点赞 评论 收藏
分享
WillingLing:查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务