题解 | #配置文件恢复#

配置文件恢复

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

写完才发现原来可以用s1.startsWith(s2),亏我还写了一大堆这玩意a[1].equals("backplane".substring(0, a[1].length()))

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String s = sc.nextLine().trim();
            String res = execCommand(s);
            System.out.println(res);
        }
    }

    public static String execCommand(String s) {
        String[] a = s.split(" ");
        if (a.length == 0 || a.length > 2) {
            return "unknown command";
        }
        if (a.length == 1) {
            if (a[0].length() < 6 &&
                    a[0].equals("reset".substring(0, a[0].length()))) {
                return "reset what";
            } else {
                return "unknown command";
            }
        }
        if (a.length == 2) {
            if (a[0].charAt(0) == 'r') {
                if (a[0].length() <= 2 && a[1].length() == 1) {
                    return "unknown command";
                }
                if (a[0].length() < 6 && a[1].length() < 6  &&
                        a[0].equals("reset".substring(0, a[0].length())) &&
                        a[1].equals("board".substring(0, a[1].length()))) {
                    return "board fault";
                } else if (a[0].length() < 7 && a[1].length() < 10  &&
                           a[0].equals("reboot".substring(0, a[0].length())) &&
                           a[1].equals("backplane".substring(0, a[1].length()))) {
                    return "impossible";
                } else {
                    return "unknown command";
                }
            } else if (a[0].charAt(0) == 'b') {
                if (a[0].equals("b") && a[1].equals("a")) {
                    return "unknown command";
                }
                if (a[0].length() < 6 && a[1].length() < 4  &&
                        a[0].equals("board".substring(0, a[0].length())) &&
                        a[1].equals("add".substring(0, a[1].length()))) {
                    return "where to add";
                }
                if (a[0].length() < 6 && a[1].length() < 7  &&
                        a[0].equals("board".substring(0, a[0].length())) &&
                        a[1].equals("delete".substring(0, a[1].length()))) {
                    return "no board at all";
                }
                if (a[0].length() < 10 && a[1].length() < 6  &&
                        a[0].equals("backplane".substring(0, a[0].length())) &&
                        a[1].equals("abort".substring(0, a[1].length()))) {
                    return "install first";
                }
                return "unknown command";
            } else {
                return "unknown command";
            }
        }
        return "unknown command";
    }
}
全部评论

相关推荐

10-09 09:39
门头沟学院 C++
HHHHaos:这也太虚了,工资就一半是真的
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务