题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
import java.util.*; // 注意类名必须为 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 a = Integer.parseInt(in.nextLine()); String actions = in.nextLine(); //初始化数据 int index = 1; int l = 1; int r = Math.min(a, 4); for (int i = 0; i < actions.length(); i++) { if (actions.charAt(i) == 'U') { //直接翻页 // 证明在翻页 if (index - 1 < l) { //证明在第一项,直接翻到前一页 if (index == 1) { index = a; r = a; l = a - Math.min(a, 4) + 1; } else { l -= 1; r -= 1; index -= 1; } } else { index -= 1; } } else if (actions.charAt(i) == 'D') { if (index + 1 > r) { //到达底部 if (index + 1 > a) { index = 1; l = 1; r = Math.min(a, 4); } else { index += 1; r += 1; l += 1; } } else { index++; } } } for (int i = l; i <= r; i++) { System.out.print(i+" "); } System.out.println(); System.out.println(index); } } }