Huawei on-Machine test (MP3 cursor movement-java)

Source: Internet
Author: User

PS: This problem is perfect for reference

Describe:

MP3 Player Because the screen is small, display the song list only a few songs per screen, the user will be able to browse all the songs through the up and down keys. To simplify processing, assume that only 4 songs per screen can be displayed, with the initial position of the cursor being the 1th song.

Now you want to navigate through the list of songs by controlling the cursor movement through the up and down keys, the control logic is as follows:

    1. The total number of songs when <=4, do not need to page, just move the cursor position.

When the cursor is on the first song, press the UP key to move to the last song; When the cursor is in the last song, press the Down key to move 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 to move the cursor to the next song.

2. When the total number of songs is greater than 4 (take a total of 10 songs for example):

Special page: The screen shows the first page (that is, the 1th - 4 ), the cursor on the first song, the user presses the UP key, the screen to display the last page (that is, the 第7-10首 song), while the cursor on the last song. Similarly, when the screen displays the last page, the cursor is on the last song, the user presses the down key, the screen displays the first page, and the light label moves to the first song.

General page: When the screen is not on the first page, the first song that the cursor displays on the current screen, when the user presses the up key, the screen starts at the top of the current song, and the cursor moves to the previous song. The down key processing is similar for the last song on the current screen of the cursor .

In other cases, do not flip the page, just move the cursor on the line.

Topic Category: string, loop, function, pointer
Difficulty: Intermediate
Scores: 100
Run time limit: Unlimited
Memory Limit: Unlimited
Stage: Apply for Exams
Input:

The first line input parameter is the total number of songs M (0 < m < 255)

The second line is input for user action, D for the down key, and u for up,d and u can be combined arbitrarily. The user's input in the test case is guaranteed to be legitimate and does not need to be verified;

For example:

10

DDDD

Represents 10 songs, the user presses the 4 down key.

Output:

Displays the 4 songs currently displayed on the MP3 screen, using [] to enclose the song in which the cursor is located;

For example:

2

3

4

[5]

Sample input:
10DDDD
Sample output:
234[5]
Answer hint:

1 ImportJava.util.Scanner;2 3  Public classMain {4 5     Private Static FinalString line_separator = System.getproperty ("Line.separator");6 7      Public Static voidMain (string[] args) {8         9Scanner cin =NewScanner (system.in);TenString Strnum =cin.nextline (); OneString Strbutton =cin.nextline (); A  - cin.close (); -          the showMp3 (Strnum, Strbutton); -          -     } -  +     Private Static voidshowMp3 (String strnum, String Strbutton) { -  +         intMusicnum =Integer.parseint (strnum); A         Char[] ch =Strbutton.tochararray (); at         intLen =ch.length; -          -         if(musicnum <= 0 | | musicnum >= 255) -         { -             return; -         } in          -         intMusic = 1; to         intStart = 1; +StringBuilder SB =NewStringBuilder (); -          the          for(inti = 0; i < Len; i++) *         { $             Switch(Ch[i])Panax Notoginseng             { -              CaseD: theMusic++; +                 if(Music = = Musicnum+1) A                 { theMusic = 1; +Start = 1; -                 } $                 Else if(Music-start > 3) $                 { -start++; -                 } the                  Break; -              CaseU:Wuyimusic--; the                 if(Music = = 0) -                 { WuMusic =Musicnum; -Start = musicNum-3; About                 } $                 Else if(Start >music) -                 { -Start =Music; -                 } A                  Break; +             default: the                 return; -             } $         } the          the         if(Musicnum < 4) the         { theStart = 1; -              for(inti = 0; i < Musicnum; i++, start++) in             { the                 if(Music = =start) the                 { AboutSb.append ("[" +music+ "]"); the                 } the                 Else the                 { + sb.append (start); -                 } the                 Bayi                 if(MusicNum-1! =i) the                 { the sb.append (line_separator); -                 } -             } the         } the         Else the         { the              for(inti = 0; I < 4; i++, start++) -             { the                 if(Music = =start) the                 { theSb.append ("[" +music+ "]");94                 } the                 Else the                 { the sb.append (start);98                 } About                  -                 if(3! =i)101                 {102 sb.append (line_separator);103                 }104             } the         }106 107 System.out.println (sb.tostring ());108     }109      the}

Huawei on-Machine test (MP3 cursor movement-java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.