题解 | #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.hasNextLine()) { // 注意 while 处理多个 case int n = Integer.parseInt(in.nextLine()); String ops = in.nextLine(); mp3Location(n, ops); } } public static void mp3Location(int n, String ops) { int[] songs = new int[n + 1]; int length = 4; int start = 0; int end = 3; int cur = start; if (n < 4) { end = n - 1; length = n; } for (int i = 0; i < songs.length; i++) { songs[i] = i; } char[] opsChars = ops.toCharArray(); for (int i = 0; i < opsChars.length; i++) { char opsC = opsChars[i]; if (opsC == 'U') { if (cur == start) { if (start == 0) { //翻页到最后 start = n - length; end = n - 1; cur = end; } else { //普通翻页 start --; end --; cur = start; } } else { cur --; } } if (opsC == 'D') { if (cur == end) { if (end == n - 1) { //翻到第一页 start = 0; end = length - 1; cur = start; } else { //普通翻页 start ++; end ++; cur = end; } } else { cur ++; } } } for (int i = start; i <= end; i++) { System.out.print((songs[i] + 1) + " "); } System.out.println(); System.out.println(cur + 1); } }