一、题目
二、示例
三、代码
import re
while True:
try:
s = list(input().replace(' ', ''))
s[::2] = sorted(s[::2])
s[1::2] = sorted(s[1::2])
res = ""
for i in s:
if re.search(r'[0-9a-fA-F]', i):
temp = int(i, 16)
temp = int(bin(temp)[2:].rjust(4, '0')[::-1], 2)
temp = hex(temp)[2:].upper()
res += temp
else:
res += i
print(res)
except:
break
四、算法说明
-
主要学习奇数位排序,偶数位排序的方法;
-
学习进制的转换:
int(i, 16)
将字符i
转成16进制;bin(int(i, 16))[2:].rjust(4,'0')[::-1]
将十六进制转成二进制,并去除二进制开头"0b"
,如果二进制长度小于4,则在前面补0
至四位,然后倒序。hex(temp)[2:].upper()
将上一步的二进制转成十六进制,并去除十六进制开头的"0x"
,最后再将其转成大写。
-
打印输出结果。
胡萝卜
2022年3月13日15:42:46
我不知道将去向何方,但我已在路上! |
---|
时光匆匆,虽未曾谋面,却相遇于斯,实在是莫大的缘分,感谢您的到访 ! |