题解 | #MP3光标位置#

MP3光标位置

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

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    string str;
    cin >> str;
    int result = 1;
    if (n <= 4) {
        for (auto ch : str) {
            if (ch == 'U' ) {
                if (result == 1)    result = n;
                else    result--;
            } else {
                if (result == n)    result = 1;
                else    result++;
            }
        }
        for (int i = 1; i <= n; i++)    cout << i << ' ';
        cout << endl << result;
    } else {
        int top = 1, down = 4;
        for (auto ch : str) {
            if (ch == 'U' ) {
                if (result == 1){
                    result = n;
                    down = n;
                    top = n - 3;
                }
                else if (result == top){
                    result--;
                    down--;
                    top--;
                }
                else    result--;
            } else {
                if (result == n){
                    result = 1;
                    down = 4;
                    top = 1;
                }
                else if (result == down){
                    result++;
                    down++;
                    top++;
                }
                else    result++;
            }
        }
        for (int i = top; i <= down; i++)    cout << i << ' ';
        cout << endl << result;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

02-26 15:33
已编辑
西北大学 golang
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务