张德长

导航

牛客[编程题] HJ64 MP3光标位置

HJ64 MP3光标位置
中等  通过率:24.47%  时间限制:1秒  空间限制:32M
 

描述

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

 

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

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

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

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

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

 

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

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

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

数据范围:命令长度1\le s\le 100\,歌曲数量1\le n \le 150\
进阶:时间复杂度:O(n)\,空间复杂度:O(n)\

输入描述:

输入说明:
1 输入歌曲数量
2 输入命令 U或者D

输出描述:

输出说明
1 输出当前列表
2 输出当前选中歌曲

示例1

输入:
10
UUUU
输出:
7 8 9 10
7

 

本题比较简单,只需要按照题干的逻辑逐条进行实现即可;

涉及的变量有min,max,pos,top,bottom,变量要初始化成适当的值;

 using System;
    public class Program
    {
        public static void Main()
        {
            string line;string line1 = null;string line2 = null;
            while ((line = System.Console.ReadLine()) != null)
            { // 注意 while 处理多个 case
                if (line1==null)
                {
                    line1 = line;
                }
                else
                {
                    line2 = line;
                    int min = 1;
                    int max = int.Parse(line1);
                    int pos = 1, top = 1, bottom =max<4? max:4;
                    for (int i = 0; i < line2.Length; i++)
                    {
                        if (line2[i]=='U')
                        {
                            if (max<=4)
                            {
                                pos--;
                                if (pos<min)
                                {
                                    pos += max;
                                }
                                continue;
                            }
                            if (pos==min)
                            {
                                pos = max;
                                bottom = max;
                                top = max - 3;
                                continue;
                            }
                            if (pos==top)
                            {
                                pos--;
                                top--;
                                bottom--;
                                continue;
                            }
                            pos--;
                        }
                        else if (line2[i] == 'D')
                        {
                            if (max <= 4)
                            {
                                pos++;
                                if (pos >max)
                                {
                                    pos -= max;
                                }
                                continue;
                            }
                            if (pos==max)
                            {
                                pos = min;
                                top = min;
                                bottom = min + 3;
                                continue;
                            }
                            if (pos == bottom)
                            {
                                pos++;
                                top++;
                                bottom++;
                                continue;
                            }
                            pos++;
                        }
                        Console.WriteLine($"pos={pos},top={top},bottom={bottom}");
                    }
                    string res = string.Empty;
                    for (int i = top; i < bottom; i++)
                    {
                        res += i.ToString()+" ";
                    }
                    res += bottom.ToString();
                    Console.WriteLine(res);
                    Console.WriteLine(pos);
                    
                }
            }
        }
    }

 

posted on 2023-11-11 22:19  张德长  阅读(13)  评论(0编辑  收藏  举报