题解 | #配置文件恢复#

配置文件恢复

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

import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    private static Map<String[], String> map = new HashMap<>();
    static {
        map.put(new String[]{"reset"}, "reset what");
        map.put(new String[]{"reset","board"}, "board fault");
        map.put(new String[]{"board","add"}, "where to add");
        map.put(new String[]{"board","delete"}, "no board at all");
        map.put(new String[]{"reboot","backplane"}, "impossible");
        map.put(new String[]{"backplane","abort"}, "install first");
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            process(str);
        }
    }


    private static void process(String str){
        String[] strs = str.split(" ");
        String res = null;
        for(Map.Entry<String[], String> entry : map.entrySet()) {
            String[] k = entry.getKey();
            if(strs.length != k.length) {
                continue;
            }

            boolean f = true;
            for(int i = 0; i< strs.length; i++) {
                String sim = strs[i];
                String commod = k[i];
                for(int j = 0; j< sim.length(); j++) {
                    if(sim.charAt(j) != commod.charAt(j)) {
                        f = false;
                        break;
                    }
                }
                if(!f) break;
            }
            if(f && res == null) {
                res = entry.getValue();
            } else if(f && res != null) {
                System.out.println("unknown command");
                return;
            }
        }
        if(res == null) {
            System.out.println("unknown command");
        }else {
            System.out.println(res); 
        }
          
    }





}

全部评论

相关推荐

09-23 14:45
贵州大学 财务
勇敢求职牛牛:怎么9.2佬人手一个中信证券实习
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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