MuNannm 2021-09-07 21:44 采纳率: 50%
浏览 41
已结题

牛客华为机试,一个小疑问提问

很简单的问题,但我老是出小问题,希望解答一下。题目如下:
配置文件恢复__牛客网 配置文件恢复 https://www.nowcoder.com/questionTerminal/ca6ac6ef9538419abf6f883f7d6f6ee5
我的代码:

import java.io.*;
import java.util.*;


public class Main {
    public static ArrayList<String> keyList = new ArrayList<>();
    public static ArrayList<String> valueList = new ArrayList<>();
    public static void main(String[] args) throws IOException{
        keyList.add("reset");
        keyList.add("reset board");
        keyList.add("board add");
        keyList.add("board delete");
        keyList.add("reboot backplane");
        keyList.add("backplane abort");
        valueList.add("reset what");
        valueList.add("board fault");
        valueList.add("where to add");
        valueList.add("no board at all");
        valueList.add("impossible");
        valueList.add("install first");
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String line = sc.nextLine();
            String result = getResult(line);
            System.out.println(result);
        }
    }

    public static String getResult(String line){
        if(line == null || line.length() == 0){
            return null;
        }
        String[] strs = line.split(" ");
        if(strs.length == 1){
            String s = strs[0];
            String res = getAnswer(s);
            return res;
        }else if(strs.length == 2){
            String s1 = strs[0];
            String s2 = strs[1];
            String res = getAnswer(s1,s2);
            return res;
        }else{
            return "unknown command";
        }
    }


    public static String getAnswer(String s){
        for(int i=0;i<keyList.size();i++){
            String key = keyList.get(i);
            if(key.split(" ").length >1){
                continue;
            }else if(key.startsWith(s)){
                return valueList.get(i);
            }
        }
        return "unkonwn command";
    }

    public static String getAnswer(String s1,String s2){
        int count = 0;
        int index = 0;
        for(int i=0;i<keyList.size();i++){
            if (keyList.get(i).split(" ").length != 2){
                continue;
            }
            String key1 = keyList.get(i).split(" ")[0];
            String key2 = keyList.get(i).split(" ")[1];
            if(key1.startsWith(s1)){
                if(key2.startsWith(s2)){
                    count++;
                    if(count == 1){
                        index = i;
                    }
                }
            }
        }
        if(count == 1){
            return valueList.get(index);
        }else{
            return "unkonwn command";
        }
    }
}

img

想知道为什么输出结果与预期不一致?是我的输出格式有问题吗?

  • 写回答

1条回答 默认 最新

  • 关注

    直接使用map,或者hashmap就很简单了,键值对。

    本回答被专家选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月30日
  • 专家已采纳回答 3月22日
  • 创建了问题 9月7日

悬赏问题

  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题
  • ¥20 请问有人知道怎么用工艺库里面的sdb文件通过virtuoso导出来library里面每个cell的symbol吗?
  • ¥20 海思 nnie 编译 报错
  • ¥50 决策面并仿真,要求有仿真结果图