题解 | #MP3光标位置#分情况讨论
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
import java.math.*; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import java.util.stream.*; import java.util.regex.*; import java.util.function.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int count = Integer.parseInt(in.nextLine()); String commands = in.nextLine(); // int screenSize = Math.min(4, count); int a = 1;// a is the order of all songs int b = 1;// b is the order of the songs in the screen // 分情况讨论, // 向上,a 两种情况,b 三种情况 // 向下,a 两种情况,b 三种情况 for (int i = 0; i < commands.length(); i++) { if (commands.charAt(i) == 'U') { if (a == 1 ) { a = count; b = screenSize; } else { a--; if (b != 1 ) { b--; } } } else { if (a == count) { a = 1; b = 1; } else { a++; if (b != screenSize) { b++; } } } } System.out.println(IntStream.range(a - b + 1, a - b + 1 + Math.min(count, screenSize)) .mapToObj(String::valueOf).collect(Collectors.joining(" "))); System.out.println(a); } }