题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
分情况讨论
好怀念初中时候用的mp3
#include <iostream> #include <bits/stdc++.h> #include <vector> using namespace std; const int N = 4; void do_work_under4(int n,string str); void do_work_above4(int n,string str); int main() { int n; string str; while(cin>>n>>str){ if(n<=4){ do_work_under4(n,str); } else{ do_work_above4(n,str); } } } void do_work_under4(int n,string str){ vector<int>page; for(int i = 0;i<n;i++){ page.push_back(i+1); } int index = 0; for(auto &ch:str){ if(ch=='U'){ if(index==0) index=page.size()-1; else index = index-1; } else{ if(index==page.size()-1) index = 0; else index = index+1; } } for(int &ch:page){ cout<<ch<<" "; } cout<<endl; cout<<page[index]<<endl; } void do_work_above4(int n,string str){ vector<int>page = {1,2,3,4}; int last = 3; int index = 0; for(auto &ch:str){ if(ch=='U'){ if(index==0&&page[0]==1){ index = last; page = vector<int>({n-3,n-2,n-1,n}); }else if(index==0&&page[0]!=1){ for(int &ch:page){ ch--; } }else{ //index!=0 index--; } } else if(ch=='D'){ if(index==last&&page[index]==n){ index = 0; page = vector<int>({1,2,3,4}); }else if(index==last&&page[index]!=n){ for(int &ch:page){ ch++; } }else{ //index!=last index++; } } } for(int &ch:page){ cout<<ch<<" "; } cout<<endl; cout<<page[index]<<endl; } // 64 位输出请用 printf("%lld")