1201STL應用(血型組合問題)
STL(標準模板庫)是一個高效的C++程式庫,
它包含了許多計算機科學領域里所常用的基本資料結構和基本演算法
,STL是一種型別引數化的程式設計方法,
通過STL的使用可以更好的實作代碼復用,
在STL程式設計中,容器(container)就是通用的資料結構,
容器用來承載不同型別的資料物件,就如同現實生活中,
人們使用容器用來裝載各種物品一樣,
但C++中的容器還存在一定的“資料加工能力”,
它如同一個對資料物件進行加工的模具,
可以把不同型別的資料放到這個模具中進行加工處理,
形成具有一定共同特性的資料結構,**
提示: 根據常規的血型配對表,
選取合適的STL中的容器進行設計,
對于父母雙親的血型采用向量Vector進行存盤
,對于子女可能出現的血型和不應該出現的血型采用集合Set進行存盤,
進而將父母血型和子女可能或不可能的血型進行封裝處理,
采用map容器來進行存盤
輸出要求
*如:父母血型(A + A)子女的血型 (A、O)子女不應有的血型(B、AB)
Input
A,O
Output
Possible blood type:A O Impossible blood type:AB B
Sample Input
A,O
Sample Output
Possible blood type:A O Impossible blood type:AB B
cpp
#include<bits/stdc++.h>
#include<string>
using namespace std;
string disassemble(string blood)//拆分輸入,eg:A,A 成A A
{
char charater[2];
if(blood=="B")
blood="OB";
if(blood=="A")
blood="OA";
if(blood=="O")
blood="OO";
return blood;
}
string test(string temp)//規范輸出
{
if(temp=="BA"||temp=="AB") temp="AB";
else if(temp=="AA"||temp=="OA"||temp=="AO") temp= "A";
else if(temp=="BB"||temp=="OB"||temp=="BO") temp= "B";
else if(temp=="OO") temp="O";
return temp;
}
int main()
{
string father="",mather="",temp;int gg=0;
cin>>temp;
for(int i=0;i<temp.length();i++) //分割開輸入
{
if(temp[i]!=','&& gg==0)
father+=temp[i];
else
{
if(gg==0)
{
gg=1;
i++;
}
mather+=temp[i];
}
}
vector<string> parents[2];
parents[0].push_back(disassemble(father));
parents[1].push_back(disassemble(mather));//將規范過的代碼存入
string ch1= parents[0].back();
string ch2= parents[1].back();
string H1=ch1;
string H2=ch2;
set<string> PosBle,ImPoBle;//創建set
for(int i=0;i<=1;i++)
for(int j=0;j<=1;j++) //型別轉換,否則不能正常插入test進行規范
{
char a[2];
a[0]=H1[i];
a[1]=H2[j];
string G(a,a+2);
temp = test(G); //01 10 11
PosBle.insert(temp);
}
gg=0;
cout<<"Possible blood type:";
// int n = PosBle.size();
ImPoBle.insert("AB");
ImPoBle.insert("A");
ImPoBle.insert("B");
ImPoBle.insert("O");
for(string j:PosBle)
{cout<<j<<" ";
ImPoBle.erase(j);
}
cout<<"Impossible blood type:";
for(string I:ImPoBle)
{
cout<<I<<" ";
}
set<set<string>> S;
S.insert(PosBle);
S.insert(ImPoBle);
vector<vector<string>> P;
P.push_back(parents[0]);
P.push_back(parents[1]);
map<vector<vector<string>>,set<set<string>>> MAX;//存入
/* 此處相當于map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));*/
MAX.insert(pair<vector<vector<string>>,set<set<string>>>(P, S));
}
關于map,set,vector,
集中嵌套與遍歷使用及其概況分析的綜合性問題,
很不錯,適合新手用來熟練上述的使用
整體思想較為簡單
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/278466.html
標籤:其他
