题解 | #MP3光标位置#

MP3光标位置

https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15

#include <iostream>
#include <string>
using namespace std;

//MP3光标位置
class MP3CurPos {
  private:
    //歌曲数量
    int n;

    //命令
    string s;

    //当前光标位置
    int pos;

    //当前列表
    int list[4];

  private:
    //向上
    void up(void);

    //向下
    void down(void);

  public:
    //构造函数
    MP3CurPos(const int n, const string s);

    //析构函数
    ~MP3CurPos();

    //执行命令
    MP3CurPos& execute(void);

    //输出当前列表和当前选中歌曲
    void print(void)const;
};

MP3CurPos::MP3CurPos(const int n, const string s) {
    this->n = n;
    this->s = s;
    this->pos = 1;
    for (int i = 1; (i <= this->n) && (i <= 4); i++)
        this->list[i - 1] = i;
    return;
}

MP3CurPos::~MP3CurPos() {
}

void MP3CurPos::up(void) {
    //如果当前光标位置是第一首歌曲
    if (this->pos == 1) {
        this->pos = this->n;

        //如果当前页面之外没有歌曲,当前列表不变,无需更新列表
        if (this->n <= 4)
            return;

        //如果当前页面之外还有歌曲
        if (this->n > 4) {
            for (int i = 0; i < 4; i++)
                this->list[3 - i] = this->n - i;
            return;
        }
    }

    //如果当前光标位置不是第一首歌曲
    if (this->pos != 1) {

        //如果当前光标位置不是当前列表的第一首歌曲,无需更新列表
        if (this->pos != this->list[0]) {
            this->pos--;
            return;
        }

        //如果当前光标位置是当前列表的第一首歌曲
        if (this->pos == this->list[0]) {
            this->pos--;
            for (int i = 0; i < 4; i++)
                this->list[i]--;
            return;
        }
    }
}

void MP3CurPos::down(void) {
    //如果当前光标的位置是最后一首歌曲
    if (this->pos == this->n) {
        this->pos = 1;

        //如果当前列表之外没有其他歌曲,无需更新列表
        if (this->n <= 4)
            return;

        //如果当前列表之外还有其他歌曲
        if (this->n > 4) {
            for (int i = 0; i < 4; i++)
                this->list[i] = i + 1;
            return;
        }
    }

    //如果当前光标位置不是最后一首歌曲
    if (this->pos != this->n) {

        //如果当前列表之外没有歌曲
        //提前判断,避免遇到this->list[3]未初始化的情况
        if (this->n <= 4) {
            this->pos++;
            return;
        }

        //如果当前光标位置不是当前列表的最后一首歌曲,无需更新列表
        if (this->pos != this->list[3]) {
            this->pos++;
            return;
        }

        //如果当前光标位置是当前列表的最后一首歌曲
        if (this->pos == this->list[3]) {
            this->pos++;
            for (int i = 0; i < 4; i++)
                this->list[i]++;
            return;
        }
    }
}

MP3CurPos& MP3CurPos::execute(void) {
    for (int i = 0; i < this->s.size(); i++) {
        if (this->s[i] == 'U')
            this->up();
        else if (this->s[i] == 'D')
            this->down();
    }
    // TODO: 在此处插入 return 语句
    return *this;
}

void MP3CurPos::print(void) const {
    cout << this->list[0];
    for (int i = 1; (i < 4) && (i < this->n); i++)
        cout << ' ' << this->list[i];
    cout << endl << this->pos << endl;
    return;
}

int main() {
    int n;
    string s;
    while (cin >> n >> s) { // 注意 while 处理多个 case
        MP3CurPos(n, s).execute().print();
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
我就是0offer糕手:北大不乱杀
点赞 评论 收藏
分享
缓解焦虑的最好方法是回家。鼠鼠昨天上午考完了本科阶段的最后一场考试,大概率考得稀烂,但是没多想,考完立马收拾行李,坐上了提前约好的顺风车飞奔回家。虽然家和学校很近,只有一百多公里的路程,但距离上次回家也已经有三四个月了。每次想回家,期间总有考试、毕业设计、面试、实习等等各种各样的原因,没办法回去,待在学校和公司的每一天也都充斥着无形的压力和焦虑。现在终于完成了答辩,考完了试,公司那边也请了假,是时候回去一趟了。没有提前通知爸妈,想给他们一个惊喜。下午提前到了家,他俩还在上班,只好让外公外婆来给我开门。因为我的回家,晚上外婆在厨房格外忙碌,做了满满一大桌子菜,填饱了我天天吃外卖的肚子。晚上也没空...
梦想是成为七海千秋:取决于家庭吧?其实回家更焦虑了,每天起床父母都问实习找好了没简历投递了没今天有没有面试,但是又没有什么结果,玩两下手机父母就会说你看你啥也没找到为什么天天就知道刷手机,怎么不去学习…我现在就希望我能永远在外面实习,报喜不报忧,等拿到一个好offer再回家
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务