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