题解 | #MP3光标位置#

MP3光标位置

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        musicNum = in.nextInt();
        bottomIndex = Math.min(musicNum, MUSIC_PAGE_NUM);
        in.nextLine();
        String str = in.nextLine();
        int opLength = str.length();
        char[]ops = new char[opLength];
        for (int i = 0; i < opLength; i++) {
            ops[i] = str.charAt(i);
        }
        for (int i = 0; i < ops.length; i++) {
            if (ops[i] == 'U') {
                up();
            } else {
                down();
            }
        }
        Set<Integer> set = new TreeSet<>();
        int tmp = musicIndex + 1;
        for (int i = 0; i <= screenIndex; i++) {
            set.add(tmp);
            tmp--;
        }
        tmp = musicIndex + 1;
        for (int i = screenIndex; i < bottomIndex; i++) {
            set.add(tmp);
            tmp++;
        }
        for (int i : set) {
            System.out.print(i + " ");
        }
        System.out.println();
        System.out.println(musicIndex + 1);
    }

    private static int musicNum = 10;
    private static int MUSIC_PAGE_NUM = 4;
    private static int screenIndex = 0;// 第一个位置
    private static int musicIndex = 0;
    private static int bottomIndex = Math.min(musicNum, MUSIC_PAGE_NUM);

    // 上一首
    private static void up() {
        if (screenIndex == 0) {
            if (musicIndex == 0) {
                screenIndex = bottomIndex - 1;
            } else {
                screenIndex = 0;
            }
        } else {
            screenIndex--;
        }
        if (musicIndex == 0) {
            musicIndex = musicNum - 1;
        } else {
            musicIndex--;
        }
    }
    // 下一首
    private static void down() {
        if (screenIndex == bottomIndex - 1) {
            if (musicIndex == musicNum - 1) {
                screenIndex = 0;
            } else {
                screenIndex = bottomIndex - 1;
            }
        } else {
            screenIndex++;
        }
        if (musicIndex == musicNum - 1) {
            musicIndex = 0;
        } else {
            musicIndex++;
        }
    }
}

全部评论

相关推荐

10-15 20:01
已编辑
上海大学 Java
钉钉什么垃圾公司,约面鸽人
Syca_:途虎养车给我定了我这边早上六点的笔试,睡了四个小时起来难受的要命,告诉我面试时间是两天后的凌晨四点
点赞 评论 收藏
分享
09-29 16:59
已编辑
门头沟学院 Java
牛客96609213...:疯狂背刺,之前还明确设置截止日期,还有笔试,现在一帮人卡在复筛,他反而一边开启扩招,还给扩招的免笔试,真服了,你好歹先把复筛中的给处理了再说
投递大疆等公司10个岗位
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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