题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
//需要遍历一边字符串,然后用两个标记,一个标记当前光标位置cursor,另一个标记cursor所在的行数loc
#include<iostream> #include<string> using namespace std; int main(){ int nums; cin >> nums; string inputstr; cin >> inputstr; int cursor = 1; int loc = 1;//标记光标在当前页的第几行 for(int i = 0;i<inputstr.size();++i){ if(inputstr[i] == 'U'){ --cursor; if(loc != 1)--loc;//如果是U,往上一直减到1 } else { ++cursor; if(loc != 4)++loc;//如果是D,往下一直加到4 } if(cursor == nums+1){ cursor = 1; loc = 1;//如果向下翻页了,loc挪到1 } else if(cursor == 0){ cursor = nums; loc = 4;//如果向上翻页了,loc挪到4 } } if(nums<=4){ //小于4直接输出列表就可以了 for(int i = 0;i<nums;++i){ cout << i+1 << " "; } cout << endl; cout << cursor << endl; } else{ //根据cursor和loc找到页面的头 cout << cursor-loc+1 << " "<< cursor-loc+2 << " "<< cursor-loc+3 << " "<<cursor-loc+4 << endl; cout << cursor << endl; } return 0; }