血型遗传对照表如下:
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
map <vector <string>, set <string> > mp;//建立输入血型vector, 与输出可能会出现血型set之间联系的map
string f, m;//双方血型读取
vector <string> i; //建立输入血型vector
set <string> o;//可能会出现血型set
set <string> un;//不可能会出现血型set
map <vector <string>, set <string> > unmp;//建立输入血型vector, 与不可能会出现血型set之间联系的map
void table()
{
//将全部血型输入不可能会出现血型un这一set,当输出任意可能出现血型,使用erase()将un这一set里的该血型擦除
un.insert("AB");
un.insert("A");
un.insert("B");
un.insert("O");
//应该把f, m放到打表法里面么
if (f != "AB" || m != "AB")
{
o.insert("O");
un.erase("O");
o.insert(f);
un.erase(f);
o.insert(m);
un.erase(m);
}
if ( (f == "A" && m == "B") || (f == "B" && m == "A"))
{
o.insert("AB");
o.insert("A");
o.insert("B");
un.erase("AB");
un.erase("A");
un.erase("B");
}
if ( (f == "AB" && m != "O") || (m == "AB" && f!= "O"))
{
o.insert("A");
o.insert("B");
o.insert("AB");
un.erase("AB");
un.erase("A");
un.erase("B");
}
if ( (f == "AB" && m == "O") || (m == "AB" && f == "O") )
{
o.insert("A");
o.insert("B");
un.erase("A");
un.erase("B");
}
mp[i] = o;
unmp[i] = un;
}
int main()
{
/***********************************************
string f, m;
vector <string> v;
cin >> f >> m;//这样读会读入','
************************************************/
table();
string str;
cin >> str;
string a = "";
string b = "";
int bstart = 0;
for (int z = 0; z < str.length(); z++)
{
while (str[z] != ',')
{
a += str[z];
}
if (str[z] == ',')
{
bstart = z;
}
}
for (int z = bstart; z < str.length(); z++)
{
b+= str[z];
}
i.push_back(a);
i.push_back(b);
set <string> tempi;
set <string> tempo;
for (auto it = mp.begin(); it != mp.end(); it++)//注意这一句的写法auto it = .begin(); it != .end(); it++
{
tempi = *it;
for (int i = 0; i < tempi.size(); i++)
{
if (i == 0)
{
cout << tempi[i];
}
else
{
cout << "," << tempi[i];
}
}
}
for (auto it = unmp.begin(); it != unmp.end(); it++)//注意这一句的写法auto it = .begin(); it != .end(); it++
{
tempo = *it;
for (int i = 0; i < tempo.size(); i++)
{
if (i == 0)
{
cout << tempo[i];
}
else
{
cout << "," << tempo[i];
}
}
}
}
请实现一个程序,输入父母血型,判断孩子可能的血型。
给定两个字符串father和mother,代表父母的血型,请返回一个字符串数组,代表孩子的可能血型(按照字典序排列)。
测试样例:
”A”,”A”
返回:[”A”,“O”]
当然是可以的,只要提供的key的类型满足Compare
的要求即可,C++标准规定了Compare
要求提供strict weak ordering
:
comp(a,a)==false
If comp(a,b)==true then comp(b,a)==false
if comp(a,b)==true and comp(b,c)==true then comp(a,c)==true
不好意思,最后一段话发不出来,我格式去掉:
std::map的默认comp是std::less模板,在你这里就是std::less>,里面会用<操作符比较vector的大小,并且是符合上面说的Compare要求,因为vector<>非成员函数```operator<是基于lexicographical_compare,比较结果按照string字母顺序来排
#include
#include
#include
#include
#include
using namespace std;
map , set > mp;//建立输入血型vector, 与输出可能会出现血型set之间联系的map
string f, m;//双方血型读取
vector i; //建立输入血型vector
set o;//可能会出现血型set
set un;//不可能会出现血型set
map , set > unmp;//建立输入血型vector, 与不可能会出现血型set之间联系的map
void table()
{
//将全部血型输入不可能会出现血型un这一set,当输出任意可能出现血型,使用erase()将un这一set里的该血型擦除
un.insert("AB");
un.insert("A");
un.insert("B");
un.insert("O");
//应该把f, m放到打表法里面么
if (f != "AB" || m != "AB")
{
o.insert("O");
un.erase("O");
o.insert(f);
un.erase(f);
o.insert(m);
un.erase(m);
}
if ( (f == "A" && m == "B") || (f == "B" && m == "A"))
{
o.insert("AB");
o.insert("A");
o.insert("B");
un.erase("AB");
un.erase("A");
un.erase("B");
}
if ( (f == "AB" && m != "O") || (m == "AB" && f!= "O"))
{
o.insert("A");
o.insert("B");
o.insert("AB");
un.erase("AB");
un.erase("A");
un.erase("B");
}
if ( (f == "AB" && m == "O") || (m == "AB" && f == "O") )
{
o.insert("A");
o.insert("B");
un.erase("A");
un.erase("B");
}
mp[i] = o;
unmp[i] = un;
}
int main()
{
/***********************************************
string f, m;
vector v;
cin >> f >> m;//这样读会读入','
************************************************/
table();
string str;
cin >> str;
string a = "";
string b = "";
int bstart = 0;
for (int z = 0; z < str.length(); z++)
{
while (str[z] != ',')
{
a += str[z];
}
if (str[z] == ',')
{
bstart = z;
}
}
for (int z = bstart; z < str.length(); z++)
{
b+= str[z];
}
i.push_back(a);
i.push_back(b);
set <string> tempi;
set <string> tempo;
/***********************************************
for (auto it = mp.begin(); it != mp.end(); it++)//注意这一句的写法auto it = .begin(); it != .end(); it++
{
tempi = *it; map使用有问题,map循环的it中包含两个key value,所以赋值应该用temp = it->second
for (int i = 0; i < tempi.size(); i++)
{
if (i == 0)
{
cout << tempi[i]; set不是数组,不可以按照数组的写法
}
else
{
cout << "," << tempi[i];
}
}
}
for (auto it = unmp.begin(); it != unmp.end(); it++)//注意这一句的写法auto it = .begin(); it != .end(); it++
{
tempo = *it;
for (int i = 0; i < tempo.size(); i++)
{
if (i == 0)
{
cout << tempo[i]; set不是数组,不可以按照数组的写法
}
else
{
cout << "," << tempo[i];
}
}
}
************************************************/
for (auto it = mp.begin(); it != mp.end(); it++)//注意这一句的写法auto it = .begin(); it != .end(); it++
{
tempi = it -> second; //map使用有问题,map循环的it中包含两个key value,所以赋值应该用temp = it->second
for (auto it = tempi.begin(); it != tempi.end(); it++)
{
if (it == tempi.begin())
{
cout << *it;
}
else
{
cout << "," << *it;
}
}
}
for (auto it = unmp.begin(); it != unmp.end(); it++)//注意这一句的写法auto it = .begin(); it != .end(); it++
{
tempo = it -> second; // map使用有问题,map循环的it中包含两个key value,所以赋值应该用temp = it->second
for (auto it = tempo.begin(); it != tempo.end(); it++)
{
if (it == tempo.begin())
{
cout << *it;
}
else
{
cout << "," << *it;
}
}
}
}
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
map <vector <string>, set <string> > mp;//建立输入血型vector, 与输出可能会出现血型set之间联系的map
string f, m;//双方血型读取
vector <string> i; //建立输入血型vector
set <string> o;//可能会出现血型set
set <string> un;//不可能会出现血型set
map <vector <string>, set <string> > unmp;//建立输入血型vector, 与不可能会出现血型set之间联系的map
void table()
{
//将全部血型输入不可能会出现血型un这一set,当输出任意可能出现血型,使用erase()将un这一set里的该血型擦除
un.insert("AB");
un.insert("A");
un.insert("B");
un.insert("O");
//应该把f, m放到打表法里面么
if (f != "AB" || m != "AB")
{
o.insert("O");
un.erase("O");
o.insert(f);
un.erase(f);
o.insert(m);
un.erase(m);
}
if ( (f == "A" && m == "B") || (f == "B" && m == "A"))
{
o.insert("AB");
o.insert("A");
o.insert("B");
un.erase("AB");
un.erase("A");
un.erase("B");
}
if ( (f == "AB" && m != "O") || (m == "AB" && f!= "O"))
{
o.insert("A");
o.insert("B");
o.insert("AB");
un.erase("AB");
un.erase("A");
un.erase("B");
}
if ( (f == "AB" && m == "O") || (m == "AB" && f == "O") )
{
o.insert("A");
o.insert("B");
un.erase("A");
un.erase("B");
}
mp[i] = o;
unmp[i] = un;
}
int main()
{
table();//打表法
string str;
cin >> str;
string a = "";
string b = "";
int bstart = 0;
for (int z = 0; z < str.length(); z++)
{
while (str[z] != ',')
{
a += str[z];
}
if (str[z] == ',')
{
bstart = z;
}
}
for (int z = bstart; z < str.length(); z++)
{
b+= str[z];
}
i.push_back(a);
i.push_back(b);
set <string> tempi;
set <string> tempo;
for (auto it = mp.begin(); it != mp.end(); it++)
{
tempi = it -> second;
for (auto it = tempi.begin(); it != tempi.end(); it++)
{
if (it == tempi.begin())
{
cout << *it;
}
else
{
cout << "," << *it;
}
}
}
for (auto it = unmp.begin(); it != unmp.end(); it++)
{
tempo = it -> second;
for (auto it = tempo.begin(); it != tempo.end(); it++)
{
if (it == tempo.begin())
{
cout << *it;
}
else
{
cout << "," << *it;
}
}
}
}
代码改成了这样,输入A B回车没有反应。不知道我这样写打表法是否正确,有些变量是否要定义为全局变量
不可以。map 的key 不应是一个集合。