题解 | #MP3光标位置#

MP3光标位置

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String totalNum = scanner.next();
        String operates = scanner.next();
        int num = Integer.parseInt(totalNum);
        if (num > 4) {
            Status initStatus = new Status(1, 4);
            int pointer = 1;
            for (int i = 0; i < operates.length(); i++) {
                char op = operates.charAt(i);
                if (initStatus.min == 1 && op == 'U' && pointer == 1) {
                    initStatus.UpdateStatus(num - 3, num);
                    pointer = num;
                } else if (initStatus.max == num && op == 'D' && pointer == num) {
                    initStatus.UpdateStatus(1, 4);
                    pointer = 1;
                } else if (pointer == initStatus.min && op == 'U') {
                    pointer--;
                    initStatus.UpdateStatus(initStatus.min - 1, initStatus.max - 1);
                } else if (pointer == initStatus.max && op == 'D') {
                    pointer++;
                    initStatus.UpdateStatus(initStatus.min + 1, initStatus.max + 1);
                } else {
                    if (op == 'U') {
                        pointer--;
                    }
                    if (op == 'D') {
                        pointer++;
                    }
                }
            }
            for (int i = initStatus.min; i <= initStatus.max; i++) {
                System.out.print(i + " ");
            }
            System.out.println("");
            System.out.print(pointer);
        } else if (num <= 4) {
            Status initStatus = new Status(1, num);
            int pointer = 1;
            for (int i = 0; i < operates.length(); i++) {
                char op = operates.charAt(i);
                if (op == 'U' && pointer == 1) {
                    pointer = num;
                } else if (op == 'D' && pointer == num) {
                    pointer = 1;
                } else {
                    if (op == 'U') {
                        pointer--;
                    }
                    if (op == 'D') {
                        pointer++;
                    }
                }
            }
            for (int i = initStatus.min; i <= initStatus.max; i++) {
                System.out.print(i + " ");
            }
            System.out.println("");
            System.out.print(pointer);
        }
    }
}

class Status {
    int min;
    int max;

    public Status(int min, int max) {
        this.min = min;
        this.max = max;
    }

    public void UpdateStatus(int min, int max) {
        this.min = min;
        this.max = max;
    }
}

全部评论

相关推荐

11-02 09:49
已编辑
货拉拉_测试(实习员工)
热爱生活的仰泳鲈鱼求你们别卷了:没事楼主,有反转查看图片
点赞 评论 收藏
分享
喜欢走神的孤勇者练习时长两年半:爱华,信华,等华,黑华
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务