题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
#include <stdio.h> #include <string.h> int main() { int songNum=0; scanf("%d",&songNum); char commend[109]={0}; scanf("%s",commend); int len=strlen(commend); int cursorPosition=1; if(songNum<=4) { for(int i=1;i<=songNum;i++) { printf("%d ",i); } for(int i=0;i<len;i++) { if(commend[i]=='U'&&cursorPosition==1) { cursorPosition=songNum; } else if(commend[i]=='U') { cursorPosition--; } else if(commend[i]=='D'&&cursorPosition==songNum) { cursorPosition=1; } else if(commend[i]=='D') { cursorPosition++; } } printf("\n%d",cursorPosition); } else if(songNum>4) { int screenFirstNum=1; int screenLastNum=4; for(int i=0;i<len;i++) { if(cursorPosition==1&&commend[i]=='U') { cursorPosition=songNum; screenFirstNum=songNum-3; screenLastNum=songNum; } else if(cursorPosition<screenLastNum&&commend[i]=='D') { cursorPosition++; } else if(cursorPosition==screenLastNum&&commend[i]=='D'&&cursorPosition!=songNum) { screenFirstNum++; screenLastNum++; cursorPosition++; } else if(cursorPosition==songNum&&commend[i]=='D') { cursorPosition=1; screenFirstNum=1; screenLastNum=4; } else if(cursorPosition>screenFirstNum&&commend[i]=='U') { cursorPosition--; } else if(cursorPosition==screenFirstNum&&commend[i]=='U'&&cursorPosition!=1) { screenFirstNum--; screenLastNum--; cursorPosition--; } } printf("%d %d %d %d\n",screenFirstNum,screenFirstNum+1,screenFirstNum+2,screenFirstNum+3); printf("%d",cursorPosition); } return 0; }