题解 | #MP3光标位置#

MP3光标位置

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

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

int main() {
    int n;
    string cmd;
    while (cin >> n) { // 注意 while 处理多个 case
        cin >> cmd;
        int i = 0;
        int cnt = 0;

        if (n < 4) {
            cout << 1;
            for (int i = 2; i <= n; ++i) {
                cout << ' ' << i;
            }
            for (auto ch:cmd) {
                if (ch == 'U') {
                    i = (n + i - 1) % n;
                } else {
                    i = (i + 1) % n;
                }
            }
            cout << endl;
            cout << i + 1<< endl;
        } else { 
        
            for(auto ch:cmd) {
                if (ch == 'U') {
                    if (i==0) {
                        cnt = (4 + cnt - 1) % 4;
                    }
                    else {
                        cnt = max(cnt - 1, 0);
                    }
                    i = (n + i - 1) % n;
                } else {
                    
                    if (i == n -1) {
                        cnt = (cnt + 1) % 4;
                    } else {
                        cnt = min(cnt + 1, 3);
                    }
                    i = (i + 1) % n;
                }


            }
                if (cnt == 0) {
                    cout << i + 1 <<' ' << i + 2 <<' '<< i + 3 <<' '<< i + 4 << endl;
                } else if (cnt == 1) {
                    cout << i <<' ' << i + 1 <<' '<< i + 2 <<' '<< i + 3 << endl; 
                } else if (cnt == 2) {
                    cout << i-1 <<' ' << i <<' '<< i + 1 <<' '<< i + 2 << endl; 
                } else {
                    cout << i-2 <<' ' << i-1 <<' '<< i <<' '<< i + 1 << endl;
                }
                cout << i + 1;
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务