题解 | 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 in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();// 歌曲数目
            int mp = 1;//光标初始位置
            in.nextLine();
            String ctl = in.nextLine();
            int bottom = 4;
            for (Character c : ctl.toCharArray()) {
                if (mp == 1 && c == 'U') {
                    mp = n;
                    bottom = n;
                } else if (mp == n && c == 'D') {
                    mp = 1;
                    bottom = 4;
                } else {
                    if (c == 'U') {
                        mp--;
                    } else if (c == 'D') {
                        mp++;
                    }
                    if (mp > bottom) {
                        bottom = mp;
                    } else if (bottom - mp > 3) {
                        bottom--;
                    }
                }
            }
            if (n >= 4) {
                System.out.println((bottom - 3) + " " + (bottom - 2) + " " +
                                   (bottom - 1) + " " + (bottom));
                System.out.println(mp);
            }
            if (n == 3) {
                System.out.println((bottom - 2) + " " +
                                   (bottom - 1) + " " + (bottom));
                System.out.println(mp);
            }
            if (n == 2) {
                System.out.println((bottom - 1) + " " + (bottom));
                System.out.println(mp);
            }
            if (n == 1) {
                System.out.println((bottom));
                System.out.println(mp);
            }
            // System.out.println(bottom);
        }
    }
}

On,S1,记录光标和顶部,应该是简单题

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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