题解 | #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 sc = new Scanner(System.in);
while (sc.hasNext()) {
int num = sc.nextInt();
String action = sc.next();
musicUpDown(num, action);
}
}
private static void musicUpDown(int num, String action) {
char[] acValue = action.toCharArray();
int top = 1;
int cur = 1;
for (char c : acValue) {
if (c == 'U') {
if (cur == 1) {
cur = num;
top = num > 4 ? cur - 3 : 1;
} else {
//当前光标和当页首行相等时向上滚动
if (top == cur) {
top--;
}
cur--;
}
} else {
if (cur == num) {
cur = 1;
top = 1;
} else {
//当前光标和当页尾行相等时向下滚动
if (cur == top + 3) {
top++;
}
cur++;
}
}
}
int res = Math.min(num, top + 3);
for (int i = top; i <= res; i++) {
if (i != top) {
System.out.print(" ");
}
System.out.print(i);
}
System.out.println("\n" + cur);
}
}
查看3道真题和解析