题解 | 凯撒加密-JavaScript

凯撒加密

https://www.nowcoder.com/practice/006b7917d3784371a43cfbae01a9313d

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 step = parseInt(line) // 步进数
        let ostr = await readline() // 原字符串
        const abc = Array.from({length: 26}, (_, i) => String.fromCharCode(i + 97)) // 字母a-z数组
        let res = ''
        for (let i=0; i<ostr.length; i++) {
            let index = abc.indexOf(ostr[i]) // 获取在字母数组中的位置
            let targetIndex = (index + step) % 26 // 获取+步进后在字母数组中的目标位置
            res += abc[targetIndex] // 拼接字符串
        }
        console.log(res)
    }
})();

全部评论

相关推荐

鬼迹人途:你去投一投尚游游戏,服务器一面,第一个图算法,做完了给你一个策略题,你给出方案他就提出低概率问题,答不上当场给你挂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务