数组内移动光标位置与页面位置即可
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
int n;
while(cin>>n){
string s;
cin>>s;
vector<int>res(n);
for(int i=0;i<n;i++)
res[i]=i+1;
int pos=0,head=0; //pos是光标的当前位置,head是最页面显示的第一首歌的位置
for(int i=0;i<s.size();i++){
if(s[i]=='U'){ //键盘输入命令上移
pos--;
if(head>pos) //pos位置小于head位置时,更新最终显示页面,head上移
head=pos;
if(pos<0){ //光标从页面顶部上移跳转至页面底部时
pos=n-1; //更新光标pos位置至底部
head=n-4; //更新页面位置head至底部
}
}
else if(s[i]=='D'){ //键盘输入命令下移
pos++;
if(pos==head+4) //pos与head距离大于4时,更新head至pos与head差值为4
head++;
if(pos>=n){ //光标从页面底部下移跳转至页面顶部时
pos=0; //更新光标pos位置至顶部
head=0; //更新页面位置head至顶部
}
}
}
if(n<=4) //歌曲数量小于等于4时,默认输出整个页面,head设置为页面顶部
head=0;
for(int i=0;i+head<n;i++){ //输出结果
cout<<res[head+i]<<" ";
if(i==3)
break;
}
cout<<endl<<res[pos]<<endl;
}
return 0;
}
京公网安备 11010502036488号