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

字符串合并处理

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

str1, str2 = input().split()
str3 = str1 + str2  # 拼接为一个字符串
# 把字符串变为列表
ls = list(str3)
n = len(ls)
# 按下标升序排序
for i in range(0, n, 2):  # 偶数位
    for j in range(i, n, 2):  # 冒泡排序
        if ls[i] > ls[j]:  # 把小的换到左边
            ls[i], ls[j] = ls[j], ls[i]

for i in range(1, n, 2):  # 奇数位
    for j in range(i, n, 2):  # 冒泡排序
        if ls[i] > ls[j]:  # 把小的换到左边
            ls[i], ls[j] = ls[j], ls[i]
# 转换字符
dict1 = {
    "A": 10,
    "B": 11,
    "C": 12,
    "D": 13,
    "E": 14,
    "F": 15,
    "a": 10,
    "b": 11,
    "c": 12,
    "d": 13,
    "e": 14,
    "f": 15,
}

for i in range(n):
    if ls[i] >= "0" and ls[i] <= "9":
        ls[i] = format(int(ls[i]), "04b")  # 转为二进制 format函数的返回值是字符串
        # 四位二进制翻转
        ls[i] = ls[i][::-1]
        # 翻转后的二进制字符串转十进制、再转十六进制
        ls[i] = hex(int(ls[i], 2)).upper()[2:]
    elif ls[i] in dict1:
        ls[i] = dict1[ls[i]]
        ls[i] = format(int(ls[i]), "04b")  # 转为二进制
        # 四位二进制翻转
        ls[i] = ls[i][::-1]
        # 翻转后的二进制字符串转十进制、再转十六进制
        ls[i] = hex(int(ls[i], 2)).upper()[2:]
    else:
        pass
print("".join(ls))

全部评论

相关推荐

2024-12-04 13:23
湖南工商大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务