题解 | #MP3光标位置# 滑动窗口

MP3光标位置

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

#include <iostream>
using namespace std;

void downward(int& win_left, int& win_right, int& cur_pos, int num) {
    if (cur_pos == win_right) {
        if (win_right == num) {
            win_left = 1;
            win_right = num <= 4 ? num : 4;
            cur_pos = win_left;
        } else {
            ++win_left;
            ++win_right;
            ++cur_pos;
        }
    } else {
        ++cur_pos;
    }
}

void upward(int& win_left, int& win_right, int& cur_pos, int num) {
    if (cur_pos == win_left) {
        if (win_left == 1) {
            win_left = num <= 4 ? 1 : num - 3;
            win_right = num;
            cur_pos = win_right;
        } else {
            --win_left;
            --win_right;
            --cur_pos;
        }
    } else {
        --cur_pos;
    }
}

int main() {
    int num = 0;
    cin >> num;
    string op;
    cin >> op;

    int win_left = 1;
    int win_right = num <= 4 ? num : 4;
    int cur_pos = 1;
    for (char c : op) {
        if (c == 'D') {
            downward(win_left, win_right, cur_pos, num);
        } else if (c == 'U') {
            upward(win_left, win_right, cur_pos, num);
        }
    }
    for (int i = win_left; i <= win_right; ++i) {
        cout << i << " ";
    }
    cout << endl;
    cout << cur_pos << endl;
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-21 19:05
点赞 评论 收藏
分享
头像
11-09 12:17
清华大学 C++
out11Man:小丑罢了,不用理会
点赞 评论 收藏
分享
10-30 23:23
已编辑
中山大学 Web前端
去B座二楼砸水泥地:这无论是个人素质还是专业素质都👇拉满了吧
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务