题解 | #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 的区别 musicNum = in.nextInt(); bottomIndex = Math.min(musicNum, MUSIC_PAGE_NUM); in.nextLine(); String str = in.nextLine(); int opLength = str.length(); char[]ops = new char[opLength]; for (int i = 0; i < opLength; i++) { ops[i] = str.charAt(i); } for (int i = 0; i < ops.length; i++) { if (ops[i] == 'U') { up(); } else { down(); } } Set<Integer> set = new TreeSet<>(); int tmp = musicIndex + 1; for (int i = 0; i <= screenIndex; i++) { set.add(tmp); tmp--; } tmp = musicIndex + 1; for (int i = screenIndex; i < bottomIndex; i++) { set.add(tmp); tmp++; } for (int i : set) { System.out.print(i + " "); } System.out.println(); System.out.println(musicIndex + 1); } private static int musicNum = 10; private static int MUSIC_PAGE_NUM = 4; private static int screenIndex = 0;// 第一个位置 private static int musicIndex = 0; private static int bottomIndex = Math.min(musicNum, MUSIC_PAGE_NUM); // 上一首 private static void up() { if (screenIndex == 0) { if (musicIndex == 0) { screenIndex = bottomIndex - 1; } else { screenIndex = 0; } } else { screenIndex--; } if (musicIndex == 0) { musicIndex = musicNum - 1; } else { musicIndex--; } } // 下一首 private static void down() { if (screenIndex == bottomIndex - 1) { if (musicIndex == musicNum - 1) { screenIndex = 0; } else { screenIndex = bottomIndex - 1; } } else { screenIndex++; } if (musicIndex == musicNum - 1) { musicIndex = 0; } else { musicIndex++; } } }