题解 | #MP3光标位置#

MP3光标位置

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        String str = in.nextLine();
	  //LinkedList模拟屏幕,addFirst,addLast,removeFirst,removeLast方法简明直译
        LinkedList<Integer> screen = new LinkedList<>();
        int index = 1;//光标在第几首歌
        int x = 1;//计算第一次填充屏幕用的
        int line = 1;//光标在屏幕的第几行,总共4行
        while (screen.size() < 4 && x <= n ) {//初始化屏幕
            screen.addLast(x);
            x++;
        }
        for (char c : str.toCharArray()) {
            if ('U' == c) { //按up
                if (line == 1) { //第一行上翻
                    if (index == 1) { //第一首,翻到末尾
                        index = n;
                        line = 4;
                        screen.clear();
                        int count = 0;
                        int tail = n;
                        while (count < 4 && tail > 0) {
                            screen.addFirst(tail);
                            tail--;
                            count++;
                        }
                    } else {//非第一首,上移一首即可
                        index--;
                        screen.removeLast();
                        screen.addFirst(index);
                    }
                } else {//非第一行上翻,screen不动,光标上移一行
                    index--;
                    line--;
                }

            } else {//按down
                if (line == 4) { //第四行下翻
                    if (index == n) { //末尾下翻,要转到开头
                        index = 1;
                        line = 1;
                        screen.clear();
                        for (int i = 1; i <= 4; i++) {
                            screen.addLast(i);
                        }
                    } else {//非末尾下翻
                        index++;
                        screen.removeFirst();
                        screen.addLast(index);
                    }
                } else {//非第四行下翻
                    if (index == n) { //n不足4首的情况
                        index = 1;
                        line = 1;
                    } else {//普通情况,光标下移一行
                        index++;
                        line++;
                    }
                }
            }
        }
        for (Integer item : screen) {
            System.out.print(item + " ");
        }
         System.out.println();
        System.out.println(index);
    }
}

全部评论

相关推荐

头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
“校招”、“3-5年经验”
xiaolihuamao:逆向工程不是搞外挂的吗,好像现在大学生坐牢最多的就是诈骗罪和非法侵入计算机系统罪,发美金,还居家办公,就是怕被一锅端,
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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