import java.util.Scanner; import java.util.Arrays; // 注意类名必须为 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 size = in.nextInt(); String command = in.next(); //返回两个结果,一个是光标位置,一个是当前页面展示的菜单 int index = 0; int[] songs = new int[size]; for (int i = 0; i < size; i++) { songs[i] = i + 1; } int startIndex = 0; int endIndex = Math.min(3, size - 1); for (int i = 0; i < command.length(); i++) { if (command.charAt(i) == 'D') { index++; } else if (command.charAt(i) == 'U') { index--; } else { continue; } //如果下标在子数组范围内,则不变;否则,移动数组边界 if (index >= startIndex && index <= endIndex) { continue; } //如果超过的右边界,且没超过数组尾部,则向右移动数组边界;否则,向左移动数组边界 if (index > endIndex && index < size - 1) { endIndex++; startIndex++; } else if (index < startIndex && index > 0) { startIndex--; endIndex--; } //超出数组边界,则从头或者从尾部开始 if (index < 0) { endIndex = size - 1; startIndex = Math.max(0, endIndex - 3); index = endIndex; } if (index > songs.length - 1) { startIndex = 0; endIndex = Math.min(3, size - 1); index = startIndex; } } int[] currentSongs = Arrays.copyOfRange(songs, startIndex, endIndex + 1); for (int currentSong : currentSongs) { System.out.print(currentSong + " "); } System.out.println(); //打印当前选中的歌曲 System.out.println(songs[index]); } } }