華為秋招筆試題

寫程式碼我用一陽指發表於2018-08-15

題目

  • 參加Party,統計12:00到20:00個時間段的人數。

提交程式碼

  • 筆試時自己提交的不通過程式碼:提示陣列越界
package HuaWei;

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

public class Party {
    public static void main(String[] args) {
        String input = "";
        boolean flag = true;
        while (flag){
            Scanner in = new Scanner(System.in);
            String s = in.nextLine();
            if ("-1,-1".equals(s)){
                flag = false;
            }
            input += s +" ";
        }
        for (Map.Entry<String,Integer> info:getResult(input).entrySet()) {
            System.out.println(info.getKey()+":"+info.getValue());
        }
        getResult(input);
    }

    public static Map<String,Integer> getResult(String input) {
        //初始化
        Map<String,Integer> res = new HashMap<>();
        for (int i = 12; i < 20; i++) {
            String key = "["+i+","+(i+1)+")";
            res.put(key,0);
        }
        //切分輸入
        String[] strings = input.split(" ");
        for (int i = 0; i < strings.length-1; i++) {
            String[] split = strings[i].split(",");
            Integer start = Integer.valueOf(split[0]);
            Integer end = Integer.valueOf(split[1]);
            if(start< end){
                for (int j = start; j < end; j++) {
                    String in = "["+j+","+(j+1)+")";
                    res.put(in,(Integer.valueOf(res.get(in))+1));
                }
            }
        }
        return res;
    }
}

優質答案

相關文章