题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String n = in.nextLine();
int num = Integer.parseInt(n);
String str = in.nextLine();
int top = 1; //当前光标指向哪
int[] arr = new int[5]; //当前页面那四个都是啥
if (num <= 4) {
for (int i = 1; i <= num; i++) {
arr[i] = i;
}
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'U') {
if (top == 1) {
top = num;
} else if (top > 1) {
top--;
}
} else if (str.charAt(i) == 'D') {
if (top == num) {
top = 1;
} else if (top < num) {
top++;
}
}
}
for (int i = 1; i <= num; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println(arr[top]);
} else {
for (int i = 1; i <= 4; i++) {
arr[i] = i;
}
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'U') {
if (top == 1) {
if (arr[1] == 1) {
arr[1] = num - 3;
arr[2] = num - 2;
arr[3] = num - 1;
arr[4] = num;
top = 4;
} else {
for (int j = 1; j <= 4; j++) {
arr[j] = arr[j] - 1;
}
}
} else {
top--;
}
} else if (str.charAt(i) == 'D') {
if (top == 4) {
if (arr[4] == num) {
arr[1] = 1;
arr[2] = 2;
arr[3] = 3;
arr[4] = 4;
top = 1;
} else {
for (int j = 1; j <= 4; j++) {
arr[j] = arr[j] + 1;
}
}
} else {
top++;
}
}
}
for (int i = 1; i <= 4; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println(arr[top]);
}
}
}
}
