题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
//为什么都不管小数点
let str = readline().substr(2);
const eng = {A:10,
B:11,
C:12,
D:13,
E:14,
F:15,
}
const letter = (n) => {
return n < 10 ? n:eng[n];
}
if(str.indexOf(".") != -1){
var strInt = str.split(".")[0];
var strFloat = str.split(".")[1];
var m = 0;
const maxFloat = strFloat.length-1;
for(let j = 0; j <= maxFloat;j++){
m = m + letter(strFloat[j]) * Math.pow(1/16,j+1);
}
var n = 0;
const maxInt = strInt.length-1;
for(let i = maxInt; i >= 0;i--){
n = n + letter(strInt[i]) * Math.pow(16,maxInt - i);
}
console.log(n+m);
}
else{
var n = 0;
const max = str.length-1;
for(let i = max; i >= 0;i--){
n = n + letter(str[i]) * Math.pow(16,max - i);
}
console.log(n);
}

查看8道真题和解析