当前位置:   article > 正文

华为od机试题(js篇)_js华为od机试题

js华为od机试题

01-10题

第1题:
在这里插入图片描述
答案:
const readline = require(“readline”);

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on(“line”, function (line) {

const tokens = line.split(" ");
console.log(tokens[tokens.length-1].length)
  • 1
  • 2

});

第2题:
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void (async function () {

// Write your code here
const line = await readline();
const code = await readline();

function findSum(str, s) {
    const res = str.toLocaleLowerCase();
    return res.split(s.toLocaleLowerCase()).length - 1;
}
console.log(findSum(line, code));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

})();

第3题:
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void async function () {
// Write your code here

const arr = []
while(line = await readline()){
    arr.push(line)
}
const temp = [...new Set(arr.slice(1))]
temp.sort((a,b)=>{
    return a-b
})
temp.forEach(item=>(console.log(item)))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

}()

第4题:
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void (async function () {
// Write your code here

let input = await readline();
function output(input) {
    if (input.length > 8) {
        console.log(input.substring(0, 8));
        output(input.substring(8));
    } else {
        console.log(input.padEnd(8, "0"));
    }
}
output(input);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

})();

第5题:
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void async function () {

// Write your code here
const str = await readline()
console.log(parseInt(str,16))
  • 1
  • 2
  • 3

}()

第6题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void async function () {

// Write your code here
let num = Number(await readline())
let res = []
for(let i=2;i*i<=num;i++){
    while(num%i===0){
        res.push(i)
        num/=i
    }
}
if(num>1){
    res.push(num)
}
console.log(res.join(' ').trim())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

}()

第7题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void async function () {

// Write your code here
const num = await readline()
const res = parseFloat(num)
console.log(Math.round(res))
  • 1
  • 2
  • 3
  • 4

}()

第8题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void (async function () {

// Write your code here
let obj = {};
while ((line = await readline())) {
    const list = line.split(" ");
    const key = parseInt(list[0]);
    const value = parseInt(list[1]);
    if (value) {
        if (obj[key]) {
            obj[key] += value;
        } else {
            obj[key] = value;
        }
    }
}
for (key in obj) {
    console.log(key + " " + obj[key]);
}

// // 1. 创建一个空对象来接收键值对
// let obj = {};
// // 2. 传入字符串数据
// while ((str = await readline())) {
//     // 3. 将字符串变成数组,并用一个变量接收
//     let arr = str.split(" ");
//     // 4. 用两个变量接收处理好的 arr 的两项数据,两项已经变成数字类型的数据
//     let a = parseInt(arr[0]);
//     let b = parseInt(arr[1]);

//     // 5. 判断数组 arr 的第二项数据是否为零, 为零则不符合要求
//     if (b) {
//         // 6. 如果 obj 的键(属性名)存在,或者说键名重复了 ,则键(属性名相同)的值相加
//         if (obj[a]) {
//             obj[a] += b;
//         } else {
//             // 7. 如果 obj 没有相同的 键(属性名),或者说是没有这个键名 ,就存储相应的value
//             obj[a] = b;
//         }
//     }
// }

// // 8. 遍历 obj 对象,将 键值对的值输出
// for (key in obj) {
//     console.log(key + " " + obj[key]);
// }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

})();

第9题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void async function () {

// Write your code here
while(line = await readline()){
    const list = line.toString().split('').reverse()
    const res =[...new Set(list)] 
    console.log(Number(res.join('')))
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

}()

第10题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void async function () {

// Write your code here
while(line = await readline()){
    let tokens = line.split('');
    const res = [...new Set(tokens)]
    console.log(res.length)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

}()

第11题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void async function () {

// Write your code here
while(line = await readline()){
    let tokens = line.split('').reverse();
    let res = tokens.join('')
    console.log(res)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

}()

第12题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void async function () {

// Write your code here
while(line = await readline()){
    let tokens = line.split('');
    let res = tokens.reverse().join('')
    console.log(res)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

}()

第13题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void async function () {

// Write your code here
while(line = await readline()){
    let tokens = line.split(' ');
    let res = tokens.reverse().join(' ')
    console.log(res)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

}()

第14题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void async function () {

// Write your code here
let list = []
while(line = await readline()){
    list.push(line)
}
const resList = list.sort().slice(1)
resList.forEach(item=>{
    console.log(item)
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

}()
第15题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void async function () {

// Write your code here
while(line = await readline()){
    const num = parseInt(line).toString(2)
    console.log(num.match(/1/g).length)
}
  • 1
  • 2
  • 3
  • 4
  • 5

}()

第16题
在这里插入图片描述

第17题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void async function () {
// Write your code here
let result = []
let i = 0
let j = 0
while(line = await readline()){
let list = line.split(‘;’)
list.forEach(item=>{
if(Number(item.slice(1))){
switch(item[0]){
case ‘A’:
i-=Number(item.slice(1))
break;
case ‘D’:
i+=Number(item.slice(1))
break;
case ‘S’:
j-=Number(item.slice(1))
break;
case ‘W’:
j+=Number(item.slice(1))
break;
default:
break
}
}
})
result.push(i)
result.push(j)
console.log(result.join(‘,’))
}
}()

第18题
在这里插入图片描述
第19题
在这里插入图片描述

第20题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void (async function () {

// Write your code here
while ((line = await readline())) {
    console.log(quality(line) ? "OK" : "NG");
    function quality(str) {
        let [correct, count] = [0, 0];
        if (str.length < 9 || str.includes("\n") || str.includes(" ")) return false;
        if (/[a-z]/.test(str)) {
            correct++;
        }
        if (/[A-Z]/.test(str)) {
            correct++;
        }
        if (/[1-9]/.test(str)) {
            correct++;
        }
        if (/[^\u4e00-\u9fa5a-zA-Z\d,\.,。]+/.test(str)) correct++;
        if (correct < 3) return false;
        const obj ={}
        for (let i = 0; i < str.length; i++) {
            let substring = str.substring(i, i + 3);
            if (substring.length < 3) continue;
            obj[substring] = null;
            count++;
        }
        return Object.keys(obj).length === count;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

})();
第21题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void (async function () {

// Write your code here
while ((line = await readline())) {
    let base = {
        2: "abc",
        3: "def",
        4: "ghi",
        5: "jkl",
        6: "mno",
        7: "pqrs",
        8: "tuv",
        9: "wxyz",
    };
    let str1 = line.replace(/[a-z]/g, (x) => {
        for (let key in base) {
            if (base[key].includes(x)) {
                x = key;
                return x;
            }
        }
    });
    let str2 = str1.replace(/[A-Z]/g, (x) => {
        x =
            x === "Z"
                ? 'a'
                : String.fromCharCode(x.toLowerCase().charCodeAt(0) + 1);
        return x;
        
    });
    console.log(str2);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

})();
第22题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void async function () {

// Write your code here
while(line = await readline()){
    if(line!=="0"){
        console.log(Math.floor(parseInt(line)/2))
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

}()

第23题
在这里插入图片描述
答案:
const rl = require(“readline”).createInterface({ input: process.stdin });
var iter = rlSymbol.asyncIterator;
const readline = async () => (await iter.next()).value;

void (async function () {

// Write your code here
while ((line = await readline())) {
    let tokens = line.split("");
    let result = tokens.reduce((temp, data) => {
        temp[data] = temp[data] ? temp[data] + 1 : 1;
        return temp;
    }, {});
    
    let min = 21;
    for (let index in result) {
        min = Math.min(min, result[index]);
    }
    for (let index in result) {
        if (result[index] === min) {
            const rep = new RegExp(index,'g')
            line = line.replace(rep, "");
        }
    }
    console.log(line)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

})();

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/69610
推荐阅读
相关标签
  

闽ICP备14008679号