题解 | #MP3光标位置#

MP3光标位置

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

import java.io.IOException;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        char[] commands = scanner.nextLine().toCharArray();

        int top = 1;  //第一行
        int cur = 1; //当前行
        for (char command : commands) {
            switch (command) {
                case 'U':
                    if (cur == 1) { //第一个
                        cur = n; //当前到最后一行且最后一个
                        if (cur > 4) { //超长
                            top = cur - 3; //第一行为 当前-3
                        } else { //不超长,top不变
                            top = 1;
                        }
                    } else { //其他情况
                        cur--; //当前向上走一行
                        if (top > cur) { //超过第一行
                            top = cur; //重定义第一行位置
                        }
                    }
                    break;
                case 'D':
                    if (cur == n) { //最后一行
                        top = cur = 1; //回到开始
                    } else { //其他情况
                        cur++; //当前向下走一行
                        if (cur > top + 3) { //超过窗口大小,top下移
                            top++;
                        }
                    }
                    break;
            }
        }
        for (int i = top; i<=top+3&&i<=n; i++) {
            System.out.print(i+" ");
        }
        System.out.println();
        System.out.println(cur);
    }

}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务