题解 | #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")

全部评论

相关推荐

找到实习就改名4月17日下午更改:这个在牛客不是老熟人了吗
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务