题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
char[] chars = br.readLine().toCharArray();
int head = 1; //页头歌曲
int cursor = 1; //光标歌曲
if (n <= 4) {
//仅有一页时,仅需计算光标位置即可
for (int i = 0; i < chars.length; i++) {
cursor = cursor + chars[i] == 'U' ? 1 : -1;
}
cursor = cursor % n;
cursor = cursor >= 0 ? cursor : cursor + n;
//打印列表
for (; head < n; head++) {
System.out.print(head);
System.out.print(" ");
}
System.out.println(head);
//打印光标位置
System.out.println(cursor);
return;
}
for (int i = 0; i < chars.length; i++) {
char command = chars[i];
//遇到上一首命令
if (command == 'U') {
//光标在首页第一个
if (cursor == 1) {
head = n - 3;
cursor = n;
continue;
}
//光标在其他页第一个
if (head == cursor) {
head--;
cursor--;
continue;
}
//光标在其他页非第一个
cursor--;
continue;
}
//遇到下一首命令
//光标在尾页最后一个
if (cursor == n) {
head = 1;
cursor = 1;
continue;
}
//光标在其他页最后一个
if (cursor == head + 3) {
head++;
cursor++;
continue;
}
//光标在其他页非最后一个
cursor++;
}
for (int i = 0; i < 3; i++) {
System.out.print(head++);
System.out.print(" ");
}
System.out.println(head);
System.out.println(cursor);
}
}
