首页 > 试题广场 >

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”]
map 初始化,简化代码。

class ChkBloodType {
public:
    map<string,vector<string>> rules {
        {"OO", {"O"}},
        {"AO", {"A","O"}},
        {"AA", {"A", "O"}},
        {"AB", {"A","AB","B","O"}},
        {"AAB", {"A","AB","B"}},
        {"BO", {"B","O"}},
        {"BB", {"B","O"}},
        {"BAB", {"A","AB","B"}},
        {"ABO", {"A","B"}},
        {"ABAB", {"A","AB","B"}}
    };
    vector<string> chkBlood(string father, string mother) {
        vector<string> result;
        if(rules.find(father + mother) != rules.end()){
            result = rules[father + mother];
        } else if (rules.find(mother + father) != rules.end()){
            result = rules[mother + father];
        }
        
        return result;
    }
};

发表于 2017-07-23 18:49:52 回复(1)
//C++,只贴函数部分好了
vector<int> change(string str)//把血型变成数组
{
	vector<int> vec{ 0, 0 };
	if (str.find_first_of("A") != -1)
		vec[0] = 1;
	if (str.find_first_of("B") != -1)
		vec[1] = 1;
	return vec;
}
vector<string> chkBlood(string father, string mother) {
	vector<string> child;
	vector<int> vec1 = change(father);
	vector<int> vec2 = change(mother);
	if (vec1[0] || vec2[0])  //父母双方如果存在字母"A",则子女可能为A型血
		child.push_back("A");
	if ((vec1[0] && vec2[1]) || (vec1[1] && vec2[0])) //若父母其中一方带字母“A”,另一方带“B”,则子女可能为AB
		child.push_back("AB");
	if (vec1[1] || vec2[1]) //父母双方如果存在字母"B",则子女可能为B型血
		child.push_back("B");
	if (father != "AB"&&mother != "AB") //若父母都不为"AB",则子女可能为O型血
		child.push_back("O");
	return child;
}


发表于 2017-06-18 14:02:12 回复(1)
我觉得这个题应该是让我们找出遗传的规律,不是查表吧,感觉查表写,显得有点傻
编辑于 2016-08-23 22:38:57 回复(2)
class ChkBloodType {
public:
    vector<string> chkBlood(string father, string mother) {
        // write code here
        vector <string> vec;
        if(father=="AB"||mother=="AB")
        {
            vec.push_back("A");
            if(father!="O"&&mother!="O")
            vec.push_back("AB");
            vec.push_back("B");
        }
        else
            {
            if(father=="A"||mother=="A")
                vec.push_back("A");
            if(father=="A"&&mother=="B"||father=="B"&&mother=="A")
                vec.push_back("AB");
            if(father=="B"||mother=="B")
                vec.push_back("B");
            vec.push_back("O");
        }
        return vec;
    }
};
发表于 2015-10-19 20:23:29 回复(0)
class ChkBloodType {
public:
    vector<string> chkBlood(string father, string mother) {
        vector<string> xx;
        int i,num=0;
        string fm = father+mother;
        for(i=0;i<fm.size();i++)
            num += fm[i];
        if(num < 160){
            xx.push_back("O");
            if(num == 131){
                xx.push_back("A");xx.push_back("B");xx.push_back("AB");
            }
            else if(num<157)
                if(father != "O")
                    xx.push_back(father);
                else
                    xx.push_back(mother);
        }
        else{
            xx.push_back("A");
            xx.push_back("B");
            if(num != 210)
                xx.push_back("AB");
        }
        sort(xx.begin(),xx.end());
        return xx;
    }
};
编辑于 2017-07-18 12:28:35 回复(1)
用键值对存储所有可能的结果,然后查询,注意father和mother的位置可以交换
mp = dict()
        mp["OO"] = ['O']
        mp["AO"] = ['A', 'O']
        mp["AA"] = ['A', 'O']
        mp["AB"] = ['A', 'B', 'AB', 'O']
        mp["AAB"] = ['A', 'B', 'AB']
        mp["BO"] = ['B', 'O']
        mp["BB"] = ['B', 'O']
        mp["BAB"] = ['A', 'B', 'AB']
        mp["ABO"] = ['A', 'B']
        mp["ABAB"] = ['A', 'B', 'AB']
        if father + mother not in mp:
            return sorted(mp[mother + father])
        else:
            return sorted(mp[father + mother])
写一堆条件判断也行
import java.util.*;

public class ChkBloodType {
    public String[] chkBlood(String father, String mother) {
        // write code here
        String[] parent = new String[]{father, mother};
        Arrays.sort(parent);
        if(parent[0].equals("A")){
            if(parent[1].equals("A") || parent[1].equals("O"))
                return new String[]{"A", "O"};
            else if(parent[1].equals("B"))
                return new String[]{"A", "AB", "B", "O"};
            else if(parent[1].equals("AB"))
                return new String[]{"A", "AB", "B"};
        }else if(parent[0].equals("B")){
            if(parent[1].equals("B") || parent[1].equals("O"))
                return new String[]{"B", "O"};
            else if(parent[1].equals("AB"))
                return new String[]{"A", "AB", "B"};
        }else if(parent[0].equals("AB")){
            if(parent[1].equals("O"))
                return new String[]{"A", "B"};
            else if(parent[1].equals("AB") || parent[1].equals("B"))
                return new String[]{"A", "AB", "B"};
        }
        return new String[]{"O"};
    }
}
编辑于 2021-04-09 11:28:58 回复(0)

渣渣代码

package com.special.first;

import java.util.Scanner;

/**
 * 去哪儿网05-血型遗传检测
 *
 * 分情况讨论即可:
 * 1. 两边血型长度1
 * 2. 两边血型长度为1,2或者2,1
 * 3. 两边血型长度为2,2
 * 
 * 
 * Create by Special on 2018/3/3 14:40
 */
public class Pro036 {

    public static String[] chkBlood(String father, String mother) {
        String[] result;
        if(father.length() == 1 && mother.length() == 1){
            if(father.equals(mother)){
                if(father.equals("O")) {
                    result = new String[]{"O"};
                }else{
                    result = new String[]{father, "O"};
                }
            }else if(father.equals("O") || mother.equals("O")){
                result = new String[]{father.compareTo(mother) < 0 ? father : mother, "O"};
            }else{
                result = new String[]{"A", "AB", "B", "O"};
            }
        }else if((father.length() == 1 && mother.length() == 2)
                || (father.length() == 2 && mother.length() == 1)){
            if(father.equals("O") || mother.equals("O")){
                result = new String[]{"A","B"};
            }else{
                result = new String[]{"A", "AB", "B"};
            }
        }else{
            result = new String[]{"A", "AB", "B"};
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while(input.hasNext()){
            String father = input.next();
            String mother = input.next();
            String[] result = chkBlood(father, mother);
            for(int i = 0; i < result.length; i++){
                System.out.print(result[i] + " ");
            }
            System.out.println();
        }
    }
}
发表于 2018-03-03 19:01:45 回复(0)
class ChkBloodType {
public:
    vector<string> chkBlood(string father, string mother) {         vector<string> child;         vector<int> v1 = str2vec(father);         vector<int> v2 = str2vec(mother);         if(v1[0] || v2[0])             child.push_back("A");         if((v1[0] && v2[1]) || (v1[1] && v2[0]))             child.push_back("AB");         if(v1[1] || v2[1])             child.push_back("B");         if(father!="AB" && mother!="AB")             child.push_back("O");         return child;
    }
    vector<int> str2vec(string s)
    {
        vector<int> v{0,0};
        if(s.find_first_of("A") != -1)
            v[0] = 1;
        if(s.find_first_of("B") != -1)
            v[1] = 1;
        return v;     }
};

发表于 2017-11-05 01:25:28 回复(0)
# -*- coding:utf-8 -*-
import re

class ChkBloodType:
    def chkBlood(self, father, mother):
        t = [father, mother]
        t.sort()
        k = d[tuple(t)]
        s = k.split(',')
        s.sort()
        return s

x = '''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'''
d = {}
for t in x.split('\n'):
    k = re.split('\s*', t)
    q = k[0].split('与')
    q.sort()
    d[tuple(q)] = k[1]

发表于 2017-07-02 23:36:00 回复(0)
#Pyhton版

# -*- coding:utf-8 -*-

class ChkBloodType:
    def chkBlood(self, father, mother):

        sDict = {}
        sDict['OO'] = ['O']
        sDict['AO'] = ['A','O']
        sDict['OA'] = ['A','O']
        sDict['AA'] = ['A','O']
        sDict['AB'] = ['A','AB','B','O']
        sDict['BA'] = ['A','AB','B','O']
        sDict['AAB'] = ['A','AB','B']
        sDict['ABA'] = ['A','AB','B']
        sDict['BO'] = ['B','O']
        sDict['OB'] = ['B','O']
        sDict['BB'] = ['B','O']
        sDict['BAB'] = ['A','AB','B']
        sDict['ABB'] = ['A','AB','B']
        sDict['ABO'] = ['A','B']
        sDict['OAB'] = ['A','B']
        sDict['ABAB'] = ['A','AB','B']

        return sDict.get(father+mother)

print ChkBloodType().chkBlood('A','A')


发表于 2017-03-12 14:55:33 回复(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)
class ChkBloodType {
public:
    vector<string> chkBlood(string father, string mother) {
        vector<string> ans;
 string temp = father + mother;
 if (temp.find("A") != string::npos)
  ans.push_back("A");
 if (temp.find("A") != string::npos && temp.find("B") != string::npos && temp.find("O") == string::npos)
  ans.push_back("AB");
 if (temp.find("B") != string::npos)
  ans.push_back("B");
 if (father != "AB" && mother != "AB")
  ans.push_back("O");
 return ans;
    }
}; 

发表于 2016-09-06 21:42:59 回复(1)
class ChkBloodType {
public:
    vector<string> chkBlood(string father, string mother)
    {
        vector<string> v;
        string s=father+mother;
        bool A=s.find('A')!=string::npos?1:0;
        bool B=s.find('B')!=string::npos?1:0;
        if(A)
            v.push_back("A");
        if(A&&B&&s.find('O')==string::npos)
            v.push_back("AB");
        if(B)
            v.push_back("B");
        if(s.size()==2)
            v.push_back("O");
        return v;
    }
};
发表于 2016-08-25 19:36:41 回复(0)
#python
#对血型进行编码,不用列举了
class ChkBloodType:
    def chkBlood(self, father, mother):
        dct={'A':[2,0],'B':[3,0],'AB':[2,3],'O':[0,0]}   ##血型编码
        a=dct[father]
        b=dct[mother]
        s=[]
        t=[]
        for i in a:
            for j in b:
                s.append(i+j)
        if 0 in s:
            t.append('O')
        if 2 in s or 4 in s:
            t.append('A')
        if 3 in s or 6 in s:
            t.append('B')
        if 5 in s:
            t.append('AB')
        return sorted(t)

发表于 2016-08-23 22:52:00 回复(0)
 class ChkBloodType {
public:
    vector<string> chkBlood(string father, string mother) {
        // write code here
  vector<string> result;
  int len1=father.length();
  int len2=mother.length();
  if(len1==1) father=father+"O";
  if(len2==1) mother=mother+"O";
  for(int i=0;i<2;i++)
   for(int j=0;j<2;j++){
    string temp=father.substr(i,1)+mother.substr(j,1);
    result.push_back(temp);
   }
  for(int i=0;i<result.size();i++)
   if(result[i][0]>result[i][1]) swap(result[i][0],result[i][1]);
  vector<string>::iterator it;
  for(int i=0;i<result.size();i++)
   if(result[i][1]=='O'||result[i][1]==result[i][0]) result[i].replace(0,2,1,result[i][0]);
  sort(result.begin(),result.end());
  it=unique(result.begin(),result.end());
  result.resize(distance(result.begin(),it) );
  return result;
    }
};
//yong dao sheng wu xue si xiang

发表于 2016-07-04 14:56:52 回复(0)
这种题目既然是特定的,建议是用字典存储起来,然后直接使用。要注意父母的血型是可以互换的。
public String[] chkBlood(String father, String mother) {
        // write code here
        	Map<String, String[]> bloodMap = new HashMap<String, String[]>();
		bloodMap.put("OO", new String[] { "O" });
		bloodMap.put("AO", new String[] { "A", "O" });
		bloodMap.put("AA", new String[] { "A", "O" });
		bloodMap.put("AB", new String[] { "A", "AB", "B", "O" });
		bloodMap.put("AAB", new String[] { "A", "AB", "B" });
		bloodMap.put("BO", new String[] { "B", "O" });
		bloodMap.put("BB", new String[] { "B", "O" });
		bloodMap.put("BAB", new String[] { "A", "AB", "B" });
		bloodMap.put("ABO", new String[] { "A", "B" });
		bloodMap.put("ABAB", new String[] { "A", "AB", "B" });
		if (bloodMap.get(father + mother)==null) {
			return bloodMap.get(mother+father);
		}
		return bloodMap.get(father + mother);
    }

发表于 2015-10-09 09:59:00 回复(1)

总结了一下,一共就三种情况:

  • 含AB(AB与O是特例)
  • A和B
  • 普通
# -*- coding:utf-8 -*-

class ChkBloodType:
    def chkBlood(self, father, mother):
        if 'AB' in [father, mother]:
            ans = ['A', 'AB', 'B']
            if 'O' in [father, mother]:
                ans.remove('AB')
        elif 'A' in [father, mother] and 'B' in [father, mother]:
            ans = ['A','AB', 'B', 'O']
        else:
            ans = list({father, mother, 'O'})
        return ans
编辑于 2017-04-08 16:28:42 回复(0)
参考Linux的文件权限符的表示方法(r:1,w:2,x:4)
数字化表示父母血型,确保不同血型的组合的和不同A :1,B:2, AB:4,O:8
import java.util.*;
//A   1
//B   2 
//AB  4
//O   8

public class ChkBloodType {
    public int StringtoInteger(String s){
        if(s.equals("A")) return 1;
        if(s.equals("B")) return 2;
        if(s.equals("AB")) return 4;
        if(s.equals("O")) return 8;
        else return 0;
    }
    public String[] chkBlood(String father, String mother) {
        // write code here
        Map<Integer,String[]> table = new HashMap<>();
        table.put(16,new String[]{"O"});
        table.put(9,new String[]{"A","O"});
        table.put(2,new String[]{"A","O"});
        table.put(3,new String[]{"A","AB","B","O"});
        table.put(5,new String[]{"A","AB","B"});
        table.put(10,new String[]{"B","O"});
        table.put(4,new String[]{"B","O"});
        table.put(6,new String[]{"A","AB","B"});
        table.put(12,new String[]{"A","B"});
        table.put(8,new String[]{"A","AB","B"});
        
        int sum = StringtoInteger(father) + StringtoInteger(mother);
        return table.get(sum);
        
    }
}



编辑于 2021-04-14 20:37:05 回复(0)
class ChkBloodType {
public:
    vector<string> chkBlood(string father, string mother) {
        vector<string>s;
        if(father=="O"&&mother=="O")s={"O"};
        if((father=="A"&&mother=="O")||(father=="O"&&mother=="A"))s={"A","O"};
        if(father=="A"&&mother=="A")s={"A","O"};
        if((father=="A"&&mother=="B")||(father=="B"&&mother=="A"))s={"A","AB","B","O"};
        if((father=="A"&&mother=="AB")||(father=="AB"&&mother=="A"))s={"A","AB","B"};
        if((father=="B"&&mother=="O")||(father=="O"&&mother=="B"))s={"B","O"};
        if(father=="B"&&mother=="B")s={"B","O"};
        if((father=="B"&&mother=="AB")||(father=="AB"&&mother=="B"))s={"A","AB","B"};
        if((father=="AB"&&mother=="O")||(father=="O"&&mother=="AB"))s={"A","B"};
        if(father=="AB"&&mother=="AB")s={"A","AB","B"};
        return s;
    }
};
发表于 2020-09-19 15:07:57 回复(0)
class ChkBloodType {
public:
    vector<string> chkBlood(string f, string m) {
        // write code here
        map<string,vector<string>>mp;
        mp["OO"] = vector<string>{"O"};
        mp["AO"] = vector<string>{"A","O"};
        mp["AA"] = vector<string>{"A","O"};
        mp["AB"] = vector<string>{"A","AB","B","O"};
        mp["AAB"] = vector<string>{"A","AB","B"};
        mp["BO"] = vector<string>{"B","O"};
        mp["BB"] = vector<string>{"B","O"};
        mp["BAB"] = vector<string>{"A","AB","B"};
        mp["ABO"] = vector<string>{"A","B"};
        mp["ABAB"] = vector<string>{"A","AB","B"};

        if(mp.count(f+m))
            return mp[f+m];
        if(mp.count(m+f))
            return mp[m+f];
    }
};
发表于 2020-04-12 22:18:42 回复(0)