题解 | #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.hasNext()) { // 注意 while 处理多个 case int a = in.nextInt(); String b = in.next(); int i = 0 ; int j = 1 ; int l = 1 ; int k = 4 ; if (a <= 3 ) { for (; i < b.length(); i++) { char c = b.charAt(i); if (c == 'U') { j--; } else if (c == 'D') { j++; } if (j == 0) { j = a; } else if (j > a) { j = 1 ; } } while (l <= a) { System.out.print(l + " "); l++; } System.out.println(); System.out.println(j); } else { for (; i < b.length(); i++) { char c = b.charAt(i); if (c == 'U') { j--; } else if (c == 'D') { j++; } if (j < l) { l--; k--; if (l == 0) { l = a - 3; k = a; j = a; } } else if (j > k) { l++; k++; if (k > a) { k = 4; l = 1; j = 1; } } } while (l <= k) { System.out.print(l + " "); l++; } System.out.println(); System.out.println(j); } } } }