题解 | #MP3光标位置#逻辑题

MP3光标位置

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

#include <bits/stdc++.h>
using namespace::std;

int main()
{
    int num = 0;
    string str;
    
    while (cin >> num >> str) {
        int pos = 1; // 当前位置
        int top = 1; // 页顶位置
        
        for (int i = 0;i < str.size();i++) {
            if (num <= 4) { // 小于4则无需翻页
                if ('U' == str[i]) {
                    pos--;
                } else if ('D' == str[i]) {
                    pos++;
                }
            }
            else {
                if ('U' == str[i]) {
                    pos--;
                    if (0 == pos) { // 翻到最后位置
                        top = num + 1 - 4;
                        pos = num;
                    } else {
                        if (pos < top) { // 内部上翻页
                            top = pos;
                        }
                    }
                } else if ('D' == str[i]) {
                    if (num == pos) { // 准备翻到第一页
                        top = 1;
                        pos = 1;
                    } else {
                        pos++;
                        if (pos > (top + 3)) { // 超过当前页面,则top下翻1
                            top++;
                        }
                    }
                }
            }
        }
        int cnt = (num <= 4) ? num : (top + 3);
        for (int j = top;j <= cnt;j++) {
            cout << j << " ";
        }
        cout << endl;
        cout << pos % num << endl;
    }
    
    return 0;
}
全部评论

相关推荐

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