题解 | #MP3光标位置#

MP3光标位置

http://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15

根据思路进行模拟即可

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int numOfMusic = sc.nextInt(); 
        sc.nextLine();//读到的数据后面有何\n,需要将\n清除掉
        String operations = sc.nextLine();
        int currPoint = 1; //初始化时只在第一个音乐上
        int first = 1; //列表中的第一个音乐
        if(numOfMusic<=4){
            for(int i = 0; i < operations.length(); i++){
                char o = operations.charAt(i);
                if(o == 'U'){
                    currPoint = (currPoint - 1 + numOfMusic) % numOfMusic;
                }else{
                    currPoint = (currPoint + 1 + numOfMusic) % numOfMusic;
                }
            }
        }else{
            for(int i = 0; i < operations.length(); i++){
                char o = operations.charAt(i);
                if(o == 'U'){
                    if(currPoint == first){
                        if(currPoint == 1){//需要翻到倒数第一个页
                            currPoint = numOfMusic;
                            first = numOfMusic - 3;
                        }else{//普通的向上翻页
                            currPoint = currPoint - 1;
                            first = first - 1;
                        }                     
                    }else{
                        currPoint = currPoint - 1; //普通的光标上移
                    }
                }else{
                    if(currPoint == first + 3){
                        if(first + 3 == numOfMusic){//需要翻到正数第一页
                            currPoint = 1;
                            first = 1;
                        }else{ //普通向下翻页
                            currPoint = currPoint + 1;
                            first = first + 1;
                        }
                    }else{
                        currPoint = currPoint + 1; //普通的光标下移
                    }
                }
            }
        }
        for(int i = 0; i < Math.min(numOfMusic, 4); i++){
            if(i == 0){
                System.out.print(first);
            }else{
                System.out.print(" "+(first + i));
            }
        }
        System.out.println();
        System.out.println(currPoint);
    }
    
}
全部评论

相关推荐

10-25 23:12
门头沟学院 Java
点赞 评论 收藏
分享
10-15 09:13
已编辑
天津大学 soc前端设计
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务