题解 | #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 sc = new Scanner(System.in);
        int total = sc.nextInt();
        int index = 1;
        int pos = 1;
        String directives = sc.next();
        for (int i = 0; i < directives.length(); i++) {
            char ch = directives.charAt(i);
            switch (ch) {
                case 'U':
                    if (index > 1) {
                        index -= 1;
                        if (pos == 1) {
                            pos = 1;
                        } else {
                            pos -= 1;
                        }
                    } else if (index == 1) {
                        index = total;
                        pos = Math.min(total, 4);
                    }
                    break;
                case 'D':
                    if (index == total) {
                        index = 1;
                        pos = 1;
                    } else if (index < total) {
                        index += 1;
                        if (pos == 4 || pos == total) {
                            pos = Math.min(total, 4);
                        } else {
                            pos += 1;
                        }
                    }
                    break;
            }
        }
        for (int i = 0, j = pos - 1; i < pos - 1; i++, j--) {
            System.out.print(index - j + " ");
        }
        for (int i = pos, j = 0; i <= Math.min(total, 4); i++, j++) {
            System.out.print(index + j + " ");
        }
        System.out.println();
        System.out.println(index);
    }
}

全部评论

相关推荐

鼠鼠能上岸吗:进行中是秋招大项目进行中,你还可以选别的岗位;已结束是这个岗位流程结束了;筛选中就是在简历筛选环节没hr捞
投递美团等公司10个岗位
点赞 评论 收藏
分享
09-12 12:09
郑州大学 Java
有啥题目啊?
投递小红书等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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