5-血型遺傳檢測(牛客網)
阿新 • • 發佈:2018-12-12
題目描述
血型遺傳對照表如下:
父母血型 | 子女會出現的血型 | 子女不會出現的血型 |
---|---|---|
O與O | O | A,B,AB |
A與O | A,O | B,AB |
A與A | A,O | B,AB |
A與B | A,B,AB,O | —— |
A與AB | A,B,AB | O |
B與O | B,O | A,AB |
B與B | B,O | A,AB |
B與AB | A,B,AB | O |
AB與O | A,B | O,AB |
AB與AB | A,B,AB | O |
請實現一個程式,輸入父母血型,判斷孩子可能的血型。
給定兩個字串father
測試樣例:
”A”,”A”
返回:[”A”,“O”]
import java.util.*; public class ChkBloodType { public String[] chkBlood(String father, String mother) { HashMap<String, String[]> strMap = new HashMap<>(); strMap.put("OO", new String[] { "O" }); strMap.put("AO", new String[] { "A", "O" }); strMap.put("AA", new String[] { "A", "O" }); strMap.put("AB", new String[] { "A", "AB", "B", "O" }); strMap.put("AAB", new String[] { "A", "AB", "B" }); strMap.put("BO", new String[] { "B", "O" }); strMap.put("BB", new String[] { "B", "O" }); strMap.put("BAB", new String[] { "A", "AB", "B" }); strMap.put("ABO", new String[] { "A", "B" }); strMap.put("ABAB", new String[] { "A", "AB", "B" }); String[] res = null; if (strMap.get(father + mother) == null) { res = strMap.get(mother + father); } else { res = strMap.get(father + mother); } return res; } }