题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function () {
// Write your code here
while(line = await readline()){
let tokens = parseInt(line,16);
console.log(tokens);
}
}()
我的方法
Number()函数也差不多的用法。
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function () {
// Write your code here
while(line = await readline()){
console.log (eval(line) )
}
}()
会碎觉的熊猫——的方法
eval()
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function () {
// Write your code here
while(line = await readline()){
let map = new Map()
for(let i = 0; i < 10; i++){
map.set(String(i), i)//注意:这里的String()中的S大写,是String()函数,String() 函数把对象的值转换为字符串。
}
map.set('A', 10).set('B', 11).set('C', 12).set('D', 13).set('E', 14).set('F', 15)//连续set多个值
let str = line.substring(2, line.length)//16进制的前缀是0x,数字零和英文字母X
let arr = str.toUpperCase().split('')//输入的字母不一定全是大写字母,需要转大小写
let result = 0
for(let i = 0; i < arr.length; i++){
let tempNum = Math.pow(16, arr.length - i - 1)//pow(x,y) 方法返回 x 的 y 次幂。
result += map.get(arr[i]) * tempNum
}
console.log(result)
}
}()
牛客944083017号——的方法
substring()
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function () {
// Write your code here
while ((line = await readline())) {
let arr = line.split("0x")[1].split("").reverse();
let total = 0;
let str1 = ["a", "b", "c", "d", "e", "f"];
let str2 = ["A", "B", "C", "D", "E", "F"];
arr.forEach((item, index) => {
let num =
str1.indexOf(item) != -1
? str1.indexOf(item) + 10
: str2.indexOf(item) != -1
? str2.indexOf(item) + 10
: parseInt(item);//太会用三目运算符了吧!!!
while (index > 0) {
num = num * 16;
index--;
}
total = total + num;
});
console.log(total);
}
}()
{&}——的方法
练练练练练 文章被收录于专栏
练练练练练