1. 程式人生 > >【去哪兒】血型遺傳檢測

【去哪兒】血型遺傳檢測

題目描述

血型遺傳對照表如下:

父母血型 子女會出現的血型 子女不會出現的血型
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”]
class ChkBloodType {
public:
    vector<string> chkBlood(string father, string mother) {
        // write code here
       vector<string> ans;
       if(father.size()==mother.size()&&father.size()==1)
       {
               ans.push_back("O");
               if(father[0]=='O'&&mother[0]=='O')
               {    
               }
               else
               {
                   if(father[0]==mother[0])
                   {
                       string str;
                       ans.push_back(str+father[0]);
                   }
                   else
                   {
                        if(father[0]!='O'&&mother[0]!='O')
                        {
                              ans.push_back("A");
                              ans.push_back("B");
                              ans.push_back("AB");
                        }
                       else
                           ans.push_back(father[0]=='O'?mother:father);
                   }

               }
           
       }
        else
        {
            ans.push_back("A");
            ans.push_back("B");
            if(father[0]!='O'&&mother[0]!='O')
                ans.push_back("AB");           
        }
        sort(ans.begin(),ans.end());
        return ans;
    }
};