题解 | #MP3光标位置# 模拟窗口和光标移动
MP3光标位置
http://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
注意边界情况。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
int music[N];
int m;
int hh, tt, idx;
inline void move2(char c)
{
    if (c == 'U') {
        if (idx > 1)
            idx--;
        else 
            idx = m;
    }
    else if (c == 'D') {
        if (idx < 3)
            idx++;
        else
            idx = 1;
    }
}
inline void move(char c)
{
    if (idx == 1 && c == 'U') {
        hh = m - 3, tt = m, idx = m;
    }
    else if(idx == m && c == 'D') {
        hh = 1, tt = 4, idx = 1;
    }
    else if (idx == hh && c == 'U') {
        hh--, tt--, idx = hh;
    }
    else if (idx == tt && c == 'D') {
        hh++, tt++, idx = tt;
    }
    else {
        if (c == 'U') {
            idx--;
        } 
        else if (c == 'D') {
            idx++;
        }
    }
    //cout << "hh: " << hh << " tt: " << tt << " idx: " << idx << endl; 
}
inline void init(int n)
{
    hh = 1, tt = n < 4 ? n : 4, idx = 1;
    for (int i = 1; i <= n; i++) {
        music[i] = i;
    }
}
int main()
{
    int n;
    string opt;
    for (; cin >> n >> opt; memset(music, 0, sizeof(music))) {
        m = n;
        init(n);
        if (n < 4)
            for (const auto &c : opt)
                move2(c);
        else 
            for (const auto &c : opt)
                move(c);
        for (int i = hh; i <= tt; i++) 
            cout << i << " ";
        cout << endl << idx << endl;
    }
    return 0;
}  投递宇宙厂等公司10个岗位
投递宇宙厂等公司10个岗位 查看14道真题和解析
查看14道真题和解析