题解 | #MP3光标位置#

MP3光标位置

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

要注意没有超过4的情况

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.hasNextLine()) { // 注意 while 处理多个 case
            String line = in.nextLine();
            int n = Integer.parseInt(line);
            String cmd = in.nextLine();
            int index = 0;
            int rs = 0;
            for(Character c : cmd.toCharArray()){
                if ( c == 'U')
                {
                    index--;
                    if (index < 0){
                       index = n - 1;
                       rs = n-4 > 0 ? n-4 : 0;
                        continue;
                    }
                    if (n > 4 && rs - 1 == index){
                        rs = index;
                    }

                }
                if (c == 'D')
                {
                    index++;
                    if (index >= n) {
                       index = 0;
                       rs = 0;
                        continue;
                    }
                    if (n > 4 && rs + 4 == index){
                        rs++;
                    }
                }
            }
            int end = rs + ((n > 4) ? 4 : n);
            for(int i=rs;i<end;i++){
                System.out.print((i+1) + " ");
            }
            System.out.print("\n");
            System.out.println((index + 1));
        }
    }
}
全部评论

相关推荐

xdm怎么说&nbsp;要被拷打了&nbsp;担心是KPI
丹田:面就完了,就当日薪四位数的大佬免费给给你面试。
点赞 评论 收藏
分享
06-27 18:53
门头沟学院 Java
这样才知道自己不适合搞代码,考公去咯
只爱喝白开水:我也发现不适合搞代码,打算转非技术方向了
点赞 评论 收藏
分享
认真搞学习:这么良心的老板真少见
点赞 评论 收藏
分享
能干的三文鱼刷了10...:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

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