题解 | #MP3光标位置#
MP3光标位置
http://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
究极屎
import java.util.*;
import java.lang.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int num = Integer.parseInt(sc.nextLine());
String cmd = sc.nextLine();
int curindex = 1;
int head = 1;
int foot = 4;
List list = new ArrayList();//当前页
for(int i=0;i<cmd.length();i++){
if(num<=4){//小于4
if(cmd.charAt(i)=='U'){//上
if(curindex==1){
curindex = num;
}else{
curindex--;
}
}
if(cmd.charAt(i)=='D'){
if(curindex==num){
curindex=1;
}else{
curindex++;
}
}
}
if(num>4){//大于4
if(cmd.charAt(i)=='U'){
if(head==1 && curindex==1){//光标在最小处 1234页
foot = num;
head = num-4+1;
curindex = num;
}else if(curindex==head && curindex!=1){//光标在头部 不等于1
curindex--;
head--;
foot--;//整个框都要更新
}else if(curindex!=head){//光标不在头部
curindex--;//仅光标移动
}
}
if(cmd.charAt(i)=='D'){
if(foot==num && curindex==num){//光标为最大值了 num为低
head = 1;
foot = head+4-1;
curindex = 1;//回框
}else if(curindex==foot&&curindex!=num){//光标在低 但是没有到最后
curindex++;
foot++;
head++;
}else if(curindex!=foot){//光标在中间
curindex++;
}
}
}
}
StringBuilder sb = new StringBuilder();
if(num<=4){
for(int i=1;i<=num;i++){
sb.append(i).append(" ");
}
}else{
for(int i = head;i<=foot;i++){
sb.append(i).append(" ");
}
}
System.out.println(sb);
System.out.println(curindex);
}
}