题解 | #MP3光标位置#

MP3光标位置

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

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()){
            int n = in.nextInt();
            String s = in.next();
            int targe = 1;
            // ********************************歌曲数<=4
            if(n<=4){
                // 输出当前列表
                for(int i=1;i<=n;i++){
                    System.out.print(i+" ");
                }
                System.out.println();
                // 输出当前选中歌曲
                for(int i=0;i<s.length();i++){
                    switch(s.charAt(i)){
                        case 'U':
                            if(targe == 1){
                                targe = n;
                            }else{
                                targe--;
                            }break;
                        case 'D':
                            if(targe == n){
                                targe = 1;
                            }else{
                                targe++;
                            }break;
                    }
                }
                System.out.println(targe);
            }
            // *********************************歌曲数>4
            else{
                // 寻找当前选中歌曲和当前列表
                int head = 1, last = 4;        // head:页面的第一首歌的编号;last:页面的最后一首歌的编号
                for(int i=0;i<s.length();i++){
                    switch(s.charAt(i)){
                        case 'U':
                            if(targe == 1){
                                targe = n;
                                head = n-3;
                                last = n;
                            }else{
                                if(targe-1 >= head){
                                    targe--;
                                }else{
                                    targe--;
                                    head--;
                                    last--;
                                }
                            }break;
                        case 'D':
                            if(targe == n){
                                targe = 1;
                                head = 1;
                                last = 4;
                            }else{
                                if(targe+1 <= last){
                                    targe++;
                                }else{
                                    targe++;
                                    head++;
                                    last++;
                                }
                            }break;
                    }
                }
                // 输出当前列表
                while(head<=last){
                    System.out.print(head+" ");
                    head++;
                }
                System.out.println();
                // 输出当前选中歌曲
                System.out.println(targe);
            }
        }
    }
}
全部评论

相关推荐

2 1 评论
分享
牛客网
牛客企业服务