题解 | #MP3光标位置#

MP3光标位置

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

#include <iostream>
using namespace std;

int main() {
    int n;
    string str;
    cin >> n >> str;
    int cur = 1, top = 1, bottom = (n <= 4) ? n : 4;
    for (auto cmd : str) {
        if (cmd == 'U') {
            if (n <= 4) {
                if (cur == top) {
                    cur = bottom;
                } else {
                    cur -= 1;
                }
            } else { // n > 4
                if (cur == top) {
                    if (cur == 1) { // jump to end
                        cur = bottom = n;
                        top = bottom - 3;
                    } else {
                        top = cur = cur - 1;
                        bottom -= 1;
                    }
                } else {
                    cur -= 1;
                }
            }
        } else if (cmd == 'D') {
            if (n <= 4) {
                if (cur == bottom) {
                    cur = top;
                } else {
                    cur += 1;
                }
            } else { // n > 4
                if (cur == bottom) {
                    if (cur == n) { // jump to begin
                        cur = top = 1;
                        bottom = top + 3;
                    } else {
                        bottom = cur = cur + 1;
                        top += 1;
                    }
                } else {
                    cur += 1;
                }
            }
        }
    }
    for (int i = top; i <= bottom; i++) {
        cout << i << " ";
    }
    cout << endl << cur << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

勉勉强强过了两题,没发挥好
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
09-11 13:00
投递长江存储等公司10个岗位
点赞 评论 收藏
分享
09-13 17:43
已编辑
北京化工大学 硬件开发
易才一飞:感觉项目写细节一些吧,掌握技能和校内经历感觉占比太大,而且这是找嵌软还是硬件呢,似乎大家都说要有针对的写相关技术才好吧
点赞 评论 收藏
分享
牛客50327486...:腾讯官方:我们没有人机对局
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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