1, Title Description
Because the screen of MP3 Player is small, when displaying the list of songs, only a few songs can be displayed on each screen. Users can browse all songs through up and down buttons. In order to simplify the processing, it is assumed that only 4 songs can be displayed on each screen, and the initial position of the cursor is the first song.
Now, to browse the song list by controlling the cursor movement with the up and down keys, the control logic is as follows:
1. When the total number of songs is < = 4, you don't need to turn the page, just move the cursor position.
When the cursor is on the first song, press the Up key to move the cursor to the last song; When the cursor is on the last song, press the Down key to move the cursor to the first song.
In other cases, the user presses the Up key and the cursor moves to the previous song; The user presses the Down key and the cursor moves to the next song.
2. When the total number of songs is greater than 4 (taking a total of 10 songs as an example):
Special page turning: when the screen displays the first page (i.e. the first to fourth songs), the cursor is on the first song. After the user presses the Up key, the screen will display the last page (i.e. the seventh to tenth songs), and the cursor will be placed on the last song at the same time. Similarly, when the screen displays the last page, the cursor is on the last song, and the user presses the Down key. The screen will display the first page, and the cursor moves to the first song.
General page turning: when the first page is not displayed on the screen, when the cursor is on the first song displayed on the current screen, after the user presses the Up key, the screen will start to display from the previous song of the current song, and the cursor will also move to the previous song. The Down key processing when the cursor is the last song on the current screen is also similar.
In other cases, you don't have to turn the page, just move the cursor.
3. Input / output description
Input Description: (1) input the number of songs; (2) Enter the command U or D
Output Description: (1) output the current song list (represented by number); (2) Output the song selected by the cursor (represented by number)
2, Problem solving
1. Problem analysis
Just pay attention to the following points:
(1) When the number of songs is less than or equal to 4, there is no need to turn the page; When the number of songs is greater than 4, you need to turn the page. So we need a constant firstSongIndex to remember the first song of each page, and another constant currTarget to record the current cursor position.
(2) Consider currTarget=1 (the cursor is in the first song). When the command is U, if n > 4, the page turning and cursor will move to the bottom page. At this time, firstSongIndex will change to n-3 (it is still in the current page when n < = 4, and there is no need to change), and currTarget will change to n (the last song);
(3) Consider currTarget=n (the cursor is in the last song). When the command is D, if n > 4, the page turning and cursor will move to the first page. At this time, firstSongIndex will change to 1 (when n < = 4, it is still in the current page and does not need to be changed), and currTarget will also change to 1;
(4) In other cases, currTarget decreases with command U (move up to decrease the number) and increases with command D; However, the firstSongIndex will change only when turning the page. Excluding the above (2) (3), it will decrease automatically only when the command u and the cursor is in the first song on the current page, that is, when firstSongIndex==currTarget;
Or in case of command D and the cursor is on the last song of the current page, that is, when firstSongIndex+3==currTarget, it will increase automatically;
2. Code
/** * @param n Number of songs * @param cmd command */ private static void findSongsList(int n, String cmd){ int l = cmd.length(); int firstSongIndex = 1;// What is the first song on each page int curTarget = 1;// Pointer current position, which song int i = 0; while (i<l) { char curr = cmd.charAt(i); if (curr=='U'){// The current command is up // If the current curTarget points to the first song if(curTarget==1){ // When the song is greater than 4, the position of the first song needs to be changed to the first n-3 on the last page if(n>4) firstSongIndex = n-3; // Change curTarget to point to the last song curTarget = n; } else { // At this time, the cursor is moved upward, not the first one. Only when the cursor is equal to the first one // Change the position of the first page of the current page if (curTarget==firstSongIndex) firstSongIndex--; //If the current pointer curTarget does not point to the last song, it will be subtracted by one curTarget--; } } else {// The current command is down //If the current curTarget points to the last song if (curTarget==n){ // Change firstSongIndex to point to the first header firstSongIndex=1; // Change curTarget to point to the first header curTarget=1; } else { // At this time, the cursor is moved down, not the last song. Only when the cursor is the last song of the current page // The first position of the current page of the table if (curTarget==firstSongIndex+3) firstSongIndex++; //If the current pointer curTarget does not point to the last song, add one curTarget++; } } i++; } // Number of songs to show on each page int disCount = Math.min(4,n); // Print songs for the current page for (int j = 1; j <=disCount; j++, firstSongIndex++) { System.out.print(firstSongIndex); System.out.print((j==disCount) ? "\n" : " "); } // Print cursor System.out.println(curTarget); }