题解 | #MP3光标位置#

MP3光标位置

https://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.hasNextLine()) {
            int n = Integer.parseInt(in.nextLine());
            String list = in.nextLine();
            int index = 1;
            int pageIndex = 1;
            int pageSize;
            if (n >= 4) {
                pageSize = 4;
            } else {
                pageSize = n;
            }
            for (int i = 0; i < list.length(); i++) {
                switch (list.charAt(i)) {
                    case 'U':
                        if (index == 1) {
                            index = n;
                            pageIndex = pageSize;
                        } else {
                            index--;
                            if (pageIndex != 1) {
                                pageIndex--;
                            }
                        }
                        break;
                    case 'D':
                        if (index == n) {
                            index = 1;
                            pageIndex = 1;
                        } else {
                            index++;
                            if (pageIndex != pageSize) {
                                pageIndex++;
                            }
                        }
                }
            }
            for (int i = index - pageIndex+1; i < index - pageIndex + pageSize+1; i++) {
                System.out.print(i + " ");
            }
            System.out.println();
            System.out.println(index);
        }
    }
}

全部评论

相关推荐

牛客41406533...:回答他在课上学,一辈子待在学校的老教授用三十年前的祖传PPT一字一句的讲解,使用谭浩强红皮书作为教材在devc++里面敲出a+++++a的瞬间爆出114514个编译错误来学这样才显得专业
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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