题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String totalNum = scanner.next(); String operates = scanner.next(); int num = Integer.parseInt(totalNum); if (num > 4) { Status initStatus = new Status(1, 4); int pointer = 1; for (int i = 0; i < operates.length(); i++) { char op = operates.charAt(i); if (initStatus.min == 1 && op == 'U' && pointer == 1) { initStatus.UpdateStatus(num - 3, num); pointer = num; } else if (initStatus.max == num && op == 'D' && pointer == num) { initStatus.UpdateStatus(1, 4); pointer = 1; } else if (pointer == initStatus.min && op == 'U') { pointer--; initStatus.UpdateStatus(initStatus.min - 1, initStatus.max - 1); } else if (pointer == initStatus.max && op == 'D') { pointer++; initStatus.UpdateStatus(initStatus.min + 1, initStatus.max + 1); } else { if (op == 'U') { pointer--; } if (op == 'D') { pointer++; } } } for (int i = initStatus.min; i <= initStatus.max; i++) { System.out.print(i + " "); } System.out.println(""); System.out.print(pointer); } else if (num <= 4) { Status initStatus = new Status(1, num); int pointer = 1; for (int i = 0; i < operates.length(); i++) { char op = operates.charAt(i); if (op == 'U' && pointer == 1) { pointer = num; } else if (op == 'D' && pointer == num) { pointer = 1; } else { if (op == 'U') { pointer--; } if (op == 'D') { pointer++; } } } for (int i = initStatus.min; i <= initStatus.max; i++) { System.out.print(i + " "); } System.out.println(""); System.out.print(pointer); } } } class Status { int min; int max; public Status(int min, int max) { this.min = min; this.max = max; } public void UpdateStatus(int min, int max) { this.min = min; this.max = max; } }