题解 | #MP3光标位置#

MP3光标位置

https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    const n = parseInt(await readline());
    const commands = await readline();
    let start = 1,selected = 1;
    for(const c of commands){
        if(c === "U") {
            selected = (selected+n-1)%n || n;
            if(selected < start) start = selected;
            if(selected === n) start = Math.max(n-3,1);
        }else {
            selected = (selected+1)%n;
            if(selected-start> 3) start++;
            if(selected === 1) start = 1;
        }
    }
    console.log([0,1,2,3].map(item=>item+start).join(" ").slice(0,n+1));
    console.log(selected);
}()

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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