题解 | #配置文件恢复#

配置文件恢复

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

import java.util.Scanner;
//笨方法
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String[][] rbo = strings("reset board");
        String[][] rba = strings("reboot backplane");
        String[][] bad = strings("board add");
        String[][] bd = strings("board delete");
        String[][] bab = strings("backplane abort");
        while (input.hasNextLine()) {
            String a = input.nextLine();
            if (a.contains(" ")) {
			  //冲突情况
                if ((iscontain(bab, a) && iscontain(bad, a)) || (iscontain(rbo, a) && iscontain(rba, a))) {
                    System.out.println("unknown command");
                    continue;
                } else if (iscontain(rbo, a)) {
                    System.out.println("board fault");
                    continue;
                } else if (iscontain(rba, a)) {
                    System.out.println("impossible");
                    continue;
                } else if (iscontain(bad, a)) {
                    System.out.println("where to add");
                    continue;
                } else if (iscontain(bd, a)) {
                    System.out.println("no board at all");
                    continue;
                } else if (iscontain(bab, a)) {
                    System.out.println("install first");
                    continue;
                } else {
                    System.out.println("unknown command");
                    continue;
                }
            } else {
                if ((a.length() <= 5) && "reset".substring(0, a.length()).equals(a)) {
                    System.out.println("reset what");
                } else {
                    System.out.println("unknown command");
                }

            }
        }

    }

//生成二维字符串数组
    public static String[][] strings(String string) {
        String[] split = string.split(" ");
        String a = split[0];
        String b = split[1];
        String[][] array = new String[a.length()][b.length()];
        for (int i = 0; i < a.length(); i++) {
            for (int j = 0; j < b.length(); j++) {
                array[i][j] = a.substring(0, i + 1) + " " + b.substring(0, j + 1);
            }
        }
        return array;

    }

  //判断字符数组是否包含字符串
    public static boolean iscontain(String[][] array, String a) {
        for (String[] string : array) {
            for (String s : string) {
                if (a.equals(s)) {
                    return true;
                }
            }

        }
        return false;

    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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