题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, len;
string s;
cin >> n >> s;
len = s.size();
int loc = 0;
// 歌曲总数小于等于4
if (n <= 4) {
vector<int> songs(n, 0);
for (int i = 1; i <= n; i++) {
songs[i - 1] = i;
}
for (int i = 0; i < len; i++) {
if (s[i] == 'U') {
if (loc == 0) {
loc = n - 1;
} else {
loc--;
}
} else if (s[i] == 'D') {
if (loc == n - 1) {
loc = 0;
} else {
loc++;
}
}
}
for (int i = 0; i < n; i++) {
cout << songs[i] << " ";
}
cout << endl;
cout << songs[loc] << endl;
} else {
// n > 4 的情况
vector<int> songs(4);
for (int i = 1; i <= 4; i++) {
songs[i - 1] = i;
}
for (int i = 0; i < len; i++) {
if (s[i] == 'U') {
if (loc == 0 && songs[loc] == 1) {
loc = 3;
for (int j = 0; j < 4; j++) {
songs[j] += n - 4;
}
} else if (loc == 0 && songs[loc] != 1) {
for (int j = 0; j < 4; j++) {
songs[j] -= 1;
}
} else {
loc--;
}
} else if (s[i] == 'D') {
if (loc == 3 && songs[loc] == n) {
loc = 0;
for (int j = 0; j < 4; j++) {
songs[j] = j + 1;
}
} else if (loc == 3 && songs[loc] != n) {
for (int j = 0; j < 4; j++) {
songs[j] += 1;
}
} else {
loc++;
}
}
}
for (int i = 0; i < 4; i++) {
cout << songs[i] << " ";
}
cout << endl;
cout << songs[loc] << endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
本题目逻辑复杂但是实现简单,主要按照模块化的思想分别实现功能即可。


