题目:
思路:
注意进制转换
Number(‘0x’ + newStr[i]).toString(2)
Number(‘0b’ + reverseValue).toString(16)
提交:
while(line = readline()) {
let str = line.replace(/\s/g, '')
let str1 = '', str2 = ''
for(let i = 0; i < str.length; i++) {
if(i % 2 === 0) {
str1 += str[i]
} else {
str2 += str[i]
}
}
str1 = str1.split('').sort()
str2 = str2.split('').sort()
let newStr = ''
for(let i = 0; i < str1.length; i++) {
newStr += str1[i] + (str2[i] || '')
}
let result = ''
// console.log(newStr)
for(let i = 0; i < newStr.length; i++) {
if (/[a-fA-F0-9]/.test(newStr[i])) {
let reverseValue = (Number('0x' + newStr[i]).toString(2).split('').reverse().join('') + '000').slice(0, 4)
// console.log(reverseValue)
result += Number('0b' + reverseValue).toString(16).toUpperCase()
} else {
result += newStr[i]
}
}
console.log(result)
}