题解 | #MP3光标位置#

MP3光标位置

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

import java.util.Scanner;

/**
浏览歌曲:分情况讨论
歌曲数n, 屏幕显示数4
 */
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            in.nextLine();
            String s = in.nextLine();

            //光标位置位 开始为0即第一个位置
            int location = 0;
            int number = 1;  // 光标所指的歌曲序号

            if (n <= 4) { //不需要翻页,但有可能歌曲填不满
                for (char c : s.toCharArray()) {
                    if (c == 'U') {
                        //特殊情况
                        if (location == 0 && number == 1) {
                            location = n - 1;
                            number = n; //移动到最后一首歌上
                        } else {
                            location--;
                            number--;
                        }
                    }

                    if (c == 'D') {
                        if (location == n - 1 && number == n) {
                            location = 0;
                            number = 1;//移动到第一首歌上
                        } else {
                            location++;
                            number++;
                        }
                    }
                }

            } else {

                //需要翻页 - 但是不用担心歌曲填不满列表的情况
                for (char c : s.toCharArray()) {
                    if (c == 'U') {
                        if (location == 0) {
                            if (number == 1) {
                                location = 3;
                                number = n;
                            } else {
                                //只需要普通翻页
                                number--;
                            }
                        } else {
                            location--;
                            number--;
                        }
                    }

                    if (c == 'D') {
                        if (location == 3) {
                            if (number == n) {
                                location = 0;
                                number = 1;
                            } else {
                                number++;
                            }
                        } else {
                            location++;
                            number++;
                        }
                    }
                }
            }
            for (int i = 0; i < n && i < 4; i++) {
                //由于音乐是[1,n]的范围,所以可以不用额外使用数组存储当前列表中的歌曲,
                //直接根据当前光标位置,和选中的歌曲位置计算既可
                System.out.print(number - location + i + " ");
            }
            System.out.println();

            //当前选中的歌曲
            System.out.println(number);
        }
    }
}

全部评论

相关推荐

仁者伍敌:难怪小公司那么挑剔,让你们这些大佬把位置拿了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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