题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15?tpId=37&tqId=21287&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fdifficulty%3D4%26judgeStatus%3D3%26page%3D1%26pageSize%3D50%26search%3D%26tpId%3D37%26type%3D37&difficulty=3&judgeStatus=undefined&tags=&title=
import java.util.Scanner; //思路在最下面,这样分析完再写代码,还挺好 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.nextLine(); String s = in.nextLine(); getMusicList(n, s); } private static void getMusicList(int n, String options) { int current = 1; int start = 1; if (n <= 4) { for (char c : options.toCharArray()) { if (c == 'D') { if (current == n) { current = 1; } else { current++; } } else if (c == 'U') { if (current == 1) { current = n; } else { current--; } } } //输出 for (int i = 1; i <= n; i++) System.out.print(i + " "); System.out.println("\n" + current); } else { for (char c : options.toCharArray()) { if (c == 'D') { if (current == n) { current = 1; start = 1; } else if (current == start + 3 ) { current++; start++; } else { current++; } } else if (c == 'U') { if (current == 1) { current = n; start = n - 3; } else if (current == start) { current--; start--; } else { current--; } } } //输出 for (int i = start, j = 0; i <= n && j < 4; i++, j++) System.out.print(i + " "); System.out.println("\n" + current); } } }
//MP3光标位置
current 表示当前歌曲下标 从1开始
start 表示当前列表起始位置 从1开始
n<=4:
向下:
一般: current++;
特殊:current==n current=1;
向上:
一般: current--;
特殊:current==1 current = n;
n>4
向下:
一般: current++;
特殊:
为当前列表的最后一个:current == start+3 current++; start++;
为所有列表的最后一个:current==n current=1; start=1;
向上:
一般: current--;
特殊:
为当前列表的第一个:current==start current--; start--;
为所有列表的第一个:current == 1 current=n; start=n-3;