1. 程式人生 > >5-血型遺傳檢測

5-血型遺傳檢測

血型遺傳對照表如下:

父母血型

子女會出現的血型

子女不會出現的血型

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和mother,代表父母的血型,請返回一個字串陣列,代表孩子的可能血型(按照字典序排列)。

測試樣例:”A”,”A”

返回:[”A”,“O”]

 

方法:暴力法

 

解析:根據題意,利用 if-else語句進行判斷輸出即可得到所要求得的孩子可能性血型。

 

程式碼一:

#include<iostream>

#include<string>

using namespace std;



int main()

{

       string f,m;

       cin>>f>>m;

       if(f=="O"&&m=="O")

              cout<<"O"<<endl;

       else if((f=="A"&&m=="A")||(f=="A"&&m=="O")||(f=="O"&&m=="A"))

              cout<<"A,O"<<endl;

       else if((f=="A"&&m=="B")||(f=="B"&&m=="A"))

              cout<<"A,AB,B,O"<<endl;  

       else if((f=="A"&&m=="AB")||(f=="AB"&&m=="A")||(f=="AB"&&m=="B")||(f=="B"&&m=="AB")||(f=="AB"&&m=="AB"))

              cout<<"A,B,AB"<<endl;

       else if((f=="B"&&m=="B")||(f=="B"&&m=="O")||(f=="O"&&m=="B"))

              cout<<"B,O"<<endl;

       else

              cout<<"A,B"<<endl;

       return 0;

}

程式碼二:

#include<iostream>

#include<string>

#include<vector>

using namespace std;



vector<string> chkBlood(string father, string mother)

{

       vector<string> vec;

       if(father=="O"&&mother=="O")

              vec.push_back("O");

       else if((father=="A"&&mother=="A")||(father=="A"&&mother=="O")||(father=="O"&&mother=="A"))

       {

              vec.push_back("A");

              vec.push_back("O");

       }

       else if((father=="A"&&mother=="B")||(father=="B"&&mother=="A"))

       {

              vec.push_back("A");

              vec.push_back("AB");

              vec.push_back("B");

              vec.push_back("O");

       }

       else if((father=="A"&&mother=="AB")||(father=="AB"&&mother=="A")||(father=="AB"&&mother=="B")||(father=="B"&&mother=="AB")||(father=="AB"&&mother=="AB"))

       {

              vec.push_back("A");

              vec.push_back("AB");

              vec.push_back("B");

       }

       else if((father=="B"&&mother=="B")||(father=="B"&&mother=="O")||(father=="O"&&mother=="B"))

       {

              vec.push_back("B");

              vec.push_back("O");

       }

       else

       {

              vec.push_back("A");

              vec.push_back("B");

       }

       return vec;

}



int main()

{

       string f,m;

       vector<string> vec;

       cin>>f>>m;

       vec=chkBlood(f,m);

       for(int i=0;i<vec.size();i++)

              cout<<vec[i]<<" ";

       cout<<endl;

       return 0;

}