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,
集中嵌套与遍历使用及其概况分析的综合性问题,
很不错,适合新手用来熟练上述的使用
整体思想较为简单