• 华为上机测试题(MP3光标移动-java)


    PS:此题满分,可参考

     

    描述:

    MP3 Player因为屏幕较小,显示歌曲列表的时候每屏只能显示几首歌曲,用户要通过上下键才能浏览所有的歌曲。为了简化处理,假设每屏只能显示4首歌曲,光标初始的位置为第1首歌。

     

    现在要实现通过上下键控制光标移动来浏览歌曲列表,控制逻辑如下:

    1. 歌曲总数<=4的时候,不需要翻页,只是挪动光标位置。

    光标在第一首歌曲上时,按Up键光标挪到最后一首歌曲;光标在最后一首歌曲时,按Down键光标挪到第一首歌曲。

    其他情况下用户按Up键,光标挪到上一首歌曲;用户按Down键,光标挪到下一首歌曲。

      2. 歌曲总数大于4的时候(以一共有10首歌为例):

    特殊翻页:屏幕显示的是第一页(即显示第1  4首)时,光标在第一首歌曲上,用户按Up键后,屏幕要显示最后一页(即显示第7-10首歌),同时光标放到最后一首歌上。同样的,屏幕显示最后一页时,光标在最后一首歌曲上,用户按Down键,屏幕要显示第一页,光标挪到第一首歌上。

    一般翻页:屏幕显示的不是第一页时,光标在当前屏幕显示的第一首歌曲时,用户按Up键后,屏幕从当前歌曲的上一首开始显示,光标也挪到上一首歌曲。光标当前屏幕的最后一首歌时的Down键处理也类似。

    其他情况,不用翻页,只是挪动光标就行。

    题目类别: 字符串,循环,函数,指针
    难度: 中级
    分数: 100
    运行时间限制: 无限制
    内存限制: 无限制
    阶段: 应聘考试
    输入:

    第一行输入参数为歌曲总数目M(0 < M < 255)

    第二行输入为用户操作,D表示Down键,U表示UP,D和U可以随意组合。测试用例中用户的输入保证合法性,不需要校验;

    例如:

    10

    DDDD

    表示10首歌曲,用户按了4个Down键。

    输出:

    显示MP3屏幕上当前显示的4首歌曲,光标所在歌曲需要使用[]括起来;

    例如:

    2

    3

    4

    [5]

    样例输入:
    10
    DDDD
    样例输出:
    2
    3
    4
    [5]
    答案提示:

     

     

     

      1 import java.util.Scanner;
      2 
      3 public class Main {
      4 
      5     private static final String LINE_SEPARATOR = System.getProperty("line.separator");
      6 
      7     public static void main(String[] args) {
      8         
      9         Scanner cin = new Scanner(System.in);
     10         String strNum = cin.nextLine();
     11         String strButton = cin.nextLine();
     12 
     13         cin.close();
     14         
     15         showMp3(strNum, strButton);
     16         
     17     }
     18 
     19     private static void showMp3(String strNum, String strButton) {
     20 
     21         int musicNum = Integer.parseInt(strNum);
     22         char[] ch = strButton.toCharArray();
     23         int len = ch.length;
     24         
     25         if(musicNum <= 0 || musicNum >= 255)
     26         {
     27             return;
     28         }
     29         
     30         int music = 1;
     31         int start = 1;
     32         StringBuilder sb = new StringBuilder();
     33         
     34         for(int i = 0; i < len; i++)
     35         {
     36             switch(ch[i])
     37             {
     38             case 'D':
     39                 music++;
     40                 if(music == musicNum+1)
     41                 {
     42                     music = 1;
     43                     start = 1;
     44                 }
     45                 else if(music-start > 3)
     46                 {
     47                     start++;
     48                 }
     49                 break;
     50             case 'U':
     51                 music--;
     52                 if(music == 0)
     53                 {
     54                     music = musicNum;
     55                     start = musicNum-3;
     56                 }
     57                 else if(start > music)
     58                 {
     59                     start = music;
     60                 }
     61                 break;
     62             default:
     63                 return;
     64             }
     65         }
     66         
     67         if(musicNum < 4)
     68         {
     69             start = 1;
     70             for(int i = 0; i < musicNum; i++, start++)
     71             {
     72                 if(music == start)
     73                 {
     74                     sb.append("["+music+"]");
     75                 }
     76                 else
     77                 {
     78                     sb.append(start);
     79                 }
     80                 
     81                 if(musicNum-1 != i)
     82                 {
     83                     sb.append(LINE_SEPARATOR);
     84                 }
     85             }
     86         }
     87         else
     88         {
     89             for(int i = 0; i < 4; i++, start++)
     90             {
     91                 if(music == start)
     92                 {
     93                     sb.append("["+music+"]");
     94                 }
     95                 else
     96                 {
     97                     sb.append(start);
     98                 }
     99                 
    100                 if(3 != i)
    101                 {
    102                     sb.append(LINE_SEPARATOR);
    103                 }
    104             }
    105         }
    106 
    107         System.out.println(sb.toString());
    108     }
    109     
    110 }

     

     

  • 相关阅读:
    在.net 4.0程序中使用TPL Dataflow
    打算把我的视频工具整合一下
    Visual Studio 2012 Updater 2 发布了
    Entity Framework学习(二)基本操作
    Entity Framework学习(一)CodeFirst入门
    VS2012中对C++注释高亮的改进
    【翻译】(12)NDK GDB
    (3)NDK Development
    【翻译】(10)Import Module
    【翻译】(7)CPU Arch ABIs
  • 原文地址:https://www.cnblogs.com/lr-ting/p/4000967.html
Copyright © 2020-2023  润新知