题解 | #MP3光标位置#

MP3光标位置

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

用数组的方式,比用迭代器简单太多了!

思路:注意三个变量-当前音乐、当前显示列表、当前选中的显示的列表的index,给这三个变量赋个初始值,在逐个处理命令的循环体中注意分情况改变这三个变量值即可。

import java.util.Arrays;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int b = Integer.parseInt(in.nextLine());
            String a = in.nextLine();
            new Main().MP3cursor(b, a);
        }
    }
    void MP3cursor(int n, String cmd) {
        int[] out = new int[4];//最后要输出的显示列表
        int nowmusic = 1;//当前选中的音乐
        out[0] = nowmusic;
        out[1] = nowmusic + 1;
        out[2] = nowmusic + 2;
        out[3] = nowmusic + 3;
        int xuanzhongindx = 0;//当前显示列表中选中的那条的index
        for (int i = 0; i < cmd.length(); i++) {
            char cmdc = cmd.charAt(i);
            if (cmdc == 'U') {//处理命令为U的情况
                if (nowmusic == 1) {//执行命令前选中的为第一首,要整体翻页,选中项改为翻页后的最下边一条
                    nowmusic = n;
                    out[3] = nowmusic;
                    out[2] = nowmusic - 1;
                    out[1] = nowmusic - 2;
                    out[0] = nowmusic - 3;
                    xuanzhongindx = 3;
                } else {//执行命令前选中的不是第一首,当前歌曲直接切换为上一首
                    nowmusic -= 1;
                    if (xuanzhongindx == 0) {//若执行命令前选中的为最上边一条,需修改显示列表,否则只用修改选中index
                        out[0] = nowmusic;
                        out[1] = nowmusic + 1;
                        out[2] = nowmusic + 2;
                        out[3] = nowmusic + 3;
                    } else {
                        xuanzhongindx -= 1;
                    }
                }
            }
            if (cmdc == 'D') {//处理命令为D的情况
                if (nowmusic == n) {
                    nowmusic = 1;
                    out[0] = nowmusic;
                    out[1] = nowmusic + 1;
                    out[2] = nowmusic + 2;
                    out[3] = nowmusic + 3;
                    xuanzhongindx = 0;
                } else {
                    nowmusic += 1;
                    if (xuanzhongindx == 3) {
                        out[3] = nowmusic;
                        out[2] = nowmusic - 1;
                        out[1] = nowmusic - 2;
                        out[0] = nowmusic - 3;
                    } else {
                        xuanzhongindx += 1;
                    }
                }
            }
        }
        if (n <= 4) {//歌曲数量小于一页时,无论执行多少命令,显示列表都不变
            for (int i = 0; i < n; i++) {
                System.out.printf("%d ", i + 1);
            }
            System.out.println();
        } else {
            Arrays.stream(out).forEach(ff->System.out.printf("%d ", ff));
            System.out.println();
        }
        System.out.println(nowmusic);
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务