题解 | #MP3光标位置#

MP3光标位置

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner fzhinput = new Scanner(System.in);
        int num = fzhinput.nextInt(); // 歌曲总数
        fzhinput.nextLine();
        String ml = fzhinput.nextLine(); // 命令串
        
        int cursor = 1; // 光标初始位置
        int pageStart = 1; // 页面起始歌曲编号
        
        for (int i = 0; i < ml.length(); i++) {
            char command = ml.charAt(i);
            
            if (command == 'U') {
                if (cursor == 1) {
                    // 光标在第一首歌曲上时,按Up键特殊翻页到最后一页,光标到最后一首
                    cursor = num;
                    if (num > 4) pageStart = num - 3;
                } else {
                    cursor--;
                    if (cursor < pageStart){
                        pageStart--;
                    }
                }
            } else if (command == 'D') {
                if (cursor == num) {
                    // 光标在最后一首歌曲上时,按Down键特殊翻页到第一页,光标到第一首
                    cursor = 1;
                    pageStart = 1;
                } else {
                    cursor++;
                    if (cursor >= pageStart + 4){
                        pageStart++;
                    }
                }
            }
        }

        // 输出当前屏幕显示的歌曲列表
        for (int i = pageStart; i < pageStart + 4 && i <= num; i++) {
            System.out.print(i + " ");
        }
        System.out.println();
        
        // 输出当前选中的歌曲编号
        System.out.println(cursor);
        
        fzhinput.close();
    }
}

全部评论

相关推荐

05-19 19:57
蚌埠学院 Python
2237:Gpa70不算高,建议只写排名,个人技能不在多而在精,缩到8条以内。项目留一个含金量高的,减少间距弄到一页,硕士简历也就一页,本科不要写很多
实习,投递多份简历没人回...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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