#include<stdio.h>
#include<string.h>
int read()
{
char s[10];
scanf("%s",s);
int flag=0;
if(!strcmp(s,"A")) flag=1;
else if(!strcmp(s,"AB")) flag=2;
else if(!strcmp(s,"B")) flag=3;
else if(!strcmp(s,"O")) flag=4;
else if(!strcmp(s,"?")) flag=0;
return flag;
}
void print(int ans_blood[])
{
int poss=0;
for(int i=0;i<4;i++) {poss+=ans_blood[i];}
if(poss==0) printf("impossible");
else
{
printf("{");
for(int i=0;i<4;i++)
{
if(ans_blood[i]==1)
{
show(i);
poss--;
}
if(ans_blood[i]==1&&poss>0)printf(",");
}
printf("}");
}
}
void show(int type)
{
if(type==0) printf("A");
else if(type==1) printf("AB");
else if(type==2) printf("B");
else if(type==3) printf("O");
}
int main ()
{
int ans_blood[4];//输出血型可能结果的数组
int father, mother,child;
father=read();
mother=read();
child=read();
//A=0,AB=1,B=2,O=3构造三维数组
const int ABO[4][4][4]=
{1,0,0,1, 1,1,1,0, 1,1,1,1, 1,0,0,1,
1,1,1,0, 1,1,1,0, 1,1,1,0, 1,0,1,0,
1,1,1,1, 1,1,1,0, 0,0,1,1, 0,0,1,1,
1,0,0,1, 1,0,1,0, 0,0,1,1, 0,0,0,1};
if(father==0)
{
int m1 = mother-1;
int c1 = child-1;
for(int i=0;i<4;i++)
ans_blood[i]=ABO[i][m1][c1];
print(ans_blood);printf(" ");
show(m1);printf(" ");
show(c1);
}
else if(mother==0)
{
int f2 = father-1;
int c2 = child-1;
for(int i=0;i<4;i++)
ans_blood[i]=ABO[f2][i][c2];
show(f2);printf(" ");
print(ans_blood);printf(" ");
show(c2);
}
else if(child==0)
{
int m3 = mother-1;
int f3 = father-1;
for(int i=0;i<4;i++)
ans_blood[i]=ABO[f3][m3][i];
show(f3);printf(" ");
show(m3);printf(" ");
print(ans_blood);
}
}
利用三维数组,类似三维坐标系通过枚举每一种可能性构建血型可能性的数组,通过字符串的一系列读取和输出的操作实现识别血型可能性
注意strcmp函数两字符串相等时返回0