题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
#include <iostream> using namespace std; #include<string> //使用滑动窗口,分为两种情况,n<=4和n>4两种。 int main() { int n; string s; int win_b = 1; // 表示窗口开始位置 int pos = 1; //表示光标位置 int win_e; //表示窗口结束位置,窗口大小为4 while (cin >> n >> s) { if (n <= 4) { win_e = n; for (int i = 0; i < s.size(); i++) { if (s[i] == 'U') { if (pos == 1) { pos = n; } else { pos = pos - 1; } } else { if (pos == n) { pos = 1; } else { pos = pos + 1; } } } } else { win_e = 4; for (int i = 0; i < s.size(); i++) { if (s[i] == 'U') { if (pos == win_b) { if ((win_b == 1) && (pos == 1)) { win_b = n - 3; win_e = win_b + 3; pos = n; } else { pos = pos - 1; win_b = win_b - 1; win_e = win_b + 3; } } else { pos = pos - 1; } } else { if (pos == win_e) { if ((win_e == n) && (pos == n)) { win_b = 1; win_e = win_b + 3; pos = 1; } else { pos = pos + 1; win_b = win_b + 1; win_e = win_b + 3; } } else { pos = pos + 1; } } } } for(int i=win_b;i<=win_e;i++) { cout<<i<<" "; } cout<<endl; cout<<pos<<endl; } return 0; } // 64 位输出请用 printf("%lld")