首页 > 试题广场 >

5-血型遗传检测

[编程题]5-血型遗传检测
  • 热度指数:7518 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

血型遗传对照表如下:

父母血型 子女会出现的血型 子女不会出现的血型
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
请实现一个程序,输入父母血型,判断孩子可能的血型。

给定两个字符串fathermother,代表父母的血型,请返回一个字符串数组,代表孩子的可能血型(按照字典序排列)。

测试样例:
”A”,”A”
返回:[”A”,“O”]
将father和mother连在一起构成一个字符串,对该字符串进行switch分类,得到分类结果;
对于字符串数组中的空字符串即null字符串,做一次判断,判断结果中有效的数组长度len,返回准确的结果(结果中不包含null字符串)
发表于 2018-09-15 10:11:58 回复(0)
import java.util.*;

public class ChkBloodType {
    static String[][] bloods = {{"O"},{"A","O"},{"A","AB","B","O"},{"A","AB","B"},{"B","O"},{"A","B"}};
    static Map<String,Integer> bloodsIndex = new HashMap<String,Integer>();
    static{
        bloodsIndex.put("O,O",0);
        bloodsIndex.put("A,O",1);
        bloodsIndex.put("O,A",1);
        bloodsIndex.put("A,A",1);
        bloodsIndex.put("A,B",2);
        bloodsIndex.put("B,A",2);
        bloodsIndex.put("A,AB",3);
        bloodsIndex.put("AB,A",3);
        bloodsIndex.put("O,B",4);
        bloodsIndex.put("B,O",4);
        bloodsIndex.put("B,B",4);
        bloodsIndex.put("B,AB",3);
        bloodsIndex.put("AB,B",3);
        bloodsIndex.put("AB,O",5);
        bloodsIndex.put("O,AB",5);
        bloodsIndex.put("AB,AB",3);
    }
    public String[] chkBlood(String father, String mother) {
        // write code here
        String temp = father+","+mother;
        int index = bloodsIndex.get(temp);
        return bloods[index];
    }
}

发表于 2018-01-24 11:37:25 回复(0)
//按照基因规律来做。
//TreeSet本来就是按字母顺序的。数字字母可以不用写比较器
import java.util.*;

public class ChkBloodType {
    public String[] chkBlood(String father, String mother) {
        // write code here
        String[] gF = new String[2];
        if(father.equals("A")){
            gF[0]="A";
            gF[1]="i";
        }else if(father.equals("B")){
            gF[0]="B";
            gF[1]="i";
        }else if(father.equals("AB")){
            gF[0]="A";
            gF[1]="B";
        }else if(father.equals("O")){
            gF[0]="i";
            gF[1]="i";
        }
        String[] gM = new String[2];
        if(mother.equals("A")){
            gM[0]="A";
            gM[1]="i";
        }else if(mother.equals("B")){
            gM[0]="B";
            gM[1]="i";
        }else if(mother.equals("AB")){
            gM[0]="A";
            gM[1]="B";
        }else if(mother.equals("O")){
            gM[0]="i";
            gM[1]="i";
        }
        TreeSet<String> child = new TreeSet<String>();
        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++){
                child.add(gF[i]+gM[j]);
            }
        }
                                               
        //返回数组 
        TreeSet<String> set =new TreeSet<String>();
        for(String s : child){
            if(s.equals("Ai") || s.equals("iA") || s.equals("AA")){
                set.add("A");
            }else if(s.equals("Bi") || s.equals("iB") || s.equals("BB")){
                set.add("B");
            }else if(s.equals("AB") || s.equals("BA")){
                set.add("AB");
            }else if(s.equals("ii")){
                set.add("O");
            }
        }
        String[] res = new String[set.size()];
        res = set.toArray(res);
        return res;                                        
    }
}

发表于 2017-03-05 19:22:07 回复(0)