题解 | #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);
}
}