小红书暑期实习笔试
第一题字符串解密,签到题
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; (async () => { // Write your code here let str = Array.from(await readline()); const m = {}; for (let i = 0; i < 26; i++) { const code = i < 3 ? 120 + i : 94 + i; m[String.fromCharCode(97 + i)] = String.fromCharCode(code); } // console.log(m); console.log(str.map((c) => m[c]).join("")); })();
第二题k排序没思路,直接返回不在最终位置的数字数量除以k,过73%
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; (async () => { // Write your code here let T = (await readline()) * 1; while (T !== 0) { let [n, k] = (await readline()).split(" ").map((v) => v * 1); let arr = (await readline()).split(" ").map((v) => v * 1); let res = 0; for (let i = 0; i < n; i++) { if (arr[i] != i + 1) { res++; } } if (res % k !== 0) { console.log(parseInt(res / k) + 1); } else { console.log(res / k); } T--; } })();
第三题数组操作,看起来没难度但是只过了82%。。。不知道怎么优化
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; (async () => { // Write your code here let N = (await readline()) * 1; let arr = (await readline()).split(" ").map((v) => v * 1); let M = (await readline()) * 1; let L = (await readline()).split(" ").map((v) => v * 1); let R = (await readline()).split(" ").map((v) => v * 1); let modes = Array.from(await readline()); let paramX = (await readline()).split(" ").map((v) => v * 1); for (let i = 0; i < M; i++) { const mode = checkMode(modes[i]); const X = paramX[i]; for (let j = L[i]-1; j < R[i]; j++) { arr[j] = mode(arr[j], X); } } console.log(arr.join(" ")); function checkMode(mode) { if (mode === "|") { return (n, X) => n | X; } else if (mode === "&") { return (n, X) => n & X; } else { return (n, X) => X; } } })();
有没有大佬知道第二题到底怎么写的