题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { int n = Integer.parseInt(in.nextLine()); String list = in.nextLine(); int index = 1; int pageIndex = 1; int pageSize; if (n >= 4) { pageSize = 4; } else { pageSize = n; } for (int i = 0; i < list.length(); i++) { switch (list.charAt(i)) { case 'U': if (index == 1) { index = n; pageIndex = pageSize; } else { index--; if (pageIndex != 1) { pageIndex--; } } break; case 'D': if (index == n) { index = 1; pageIndex = 1; } else { index++; if (pageIndex != pageSize) { pageIndex++; } } } } for (int i = index - pageIndex+1; i < index - pageIndex + pageSize+1; i++) { System.out.print(i + " "); } System.out.println(); System.out.println(index); } } }