题解 | #MP3光标位置#分情况讨论

MP3光标位置

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

import java.math.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.stream.*;
import java.util.regex.*;
import java.util.function.*;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int count = Integer.parseInt(in.nextLine());
        String commands = in.nextLine();
        // 
        int screenSize = Math.min(4, count);

        int a = 1;// a is the order of all songs
        int b = 1;// b is the order of the songs in the screen
        // 分情况讨论, 
        // 向上,a 两种情况,b 三种情况
        // 向下,a 两种情况,b 三种情况
        for (int i = 0; i < commands.length(); i++) {
            if (commands.charAt(i) == 'U') {
                if (a == 1 ) {
                    a = count;
                    b = screenSize;
                } else {
                    a--;
                    if (b != 1 ) {
                        b--;
                    }
                }
            } else {
                if (a == count) {
                    a = 1;
                    b = 1;
                } else {
                    a++;
                    if (b != screenSize) {
                        b++;
                    }
                }
            }
        }
        System.out.println(IntStream.range(a - b + 1, a - b + 1 + Math.min(count, screenSize))
                           .mapToObj(String::valueOf).collect(Collectors.joining(" ")));
        System.out.println(a);

    }
}


全部评论

相关推荐

不愿透露姓名的神秘牛友
07-29 11:57
投递浪潮等公司10个岗位
点赞 评论 收藏
分享
07-24 03:49
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务