由于Rh血型和ABO血型的判断相互不干扰,因此可以分开来看
#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,"A-")) flag=-1;
else if(!strcmp(s,"AB+")) flag=2;
else if(!strcmp(s,"AB-")) flag=-2;
else if(!strcmp(s,"B+")) flag=3;
else if(!strcmp(s,"B-")) flag=-3;
else if(!strcmp(s,"O+")) flag=4;
else if(!strcmp(s,"O-")) flag=-4;
else if(!strcmp(s,"?")) flag=0;
return flag;
}
void print(int ans_blood[],int ans_Rh[])
{
int poss1=0;int poss2=0;
for(int i=0;i<4;i++) {poss1+=ans_blood[i];}
for(int i=0;i<2;i++) {poss2+=ans_Rh[i];}
int poss=poss1*poss2;//printf("%d",poss);
if(poss==0) printf("impossible");
else if(poss==1)
{
for(int i=0;i<4;i++)
{
if(ans_blood[i]==1)
{
if(ans_Rh[0]==1) show(i+1,0);
else if(ans_Rh[1]==1) show(-1-i,0);
}
}
}
else if(poss>1)
{
int cnt=0;
for(int i=0;i<4;i++)
{
if(ans_blood[i]==1)
{
for(int j=0;j<2;j++)
{
if(ans_Rh[j])
{
cnt++;
if(j==0)show(i+1,cnt);
else show(-1-i,cnt);
}
}
}
}
printf("}");
}
}
void show(int x,int type)
{
if(type==1) printf("{");
else if(type>1) printf(",");
if(x==1 ||x==-1 )printf("A");
if(x==2 ||x==-2 )printf("AB");
if(x==3 ||x==-3 )printf("B");
if(x==4 ||x==-4 )printf("O");
if(x>0) printf("+");
else printf("-");
}
int main ()
{
int T;
scanf("%d",&T);
for(int k=0;k<T;k++)
{
int ans_blood[4];//输出ABO血型可能结果的数组
int ans_Rh[2];//输出Rh血型可能结果的数组
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};
const int Rh[2][2][2]={ 1,1, 1,1, 1,1, 0,1};
if(father==0)
{
int m1,c1,m2,c2;
if(mother>0) {m1=mother-1;m2=0;}//0为+,1为-
else {m1=-1-mother;m2=1;}
if(child>0) {c1=child-1;c2=0;}
else {c1=-1-child;c2=1;}
for(int i=0;i<4;i++) ans_blood[i]=ABO[i][m1][c1];
for(int i=0;i<2;i++) ans_Rh[i]=Rh[i][m2][c2];
print(ans_blood,ans_Rh);printf(" ");
show(mother,0);printf(" ");
show(child,0);
}
else if(mother==0)
{
int f1,c1,f2,c2;
if(father>0) {f1=father-1;f2=0;}//0为+,1为-
else {f1=-1-father;f2=1;}
if(child>0) {c1=child-1;c2=0;}
else {c1=-1-child;c2=1;}
for(int i=0;i<4;i++) ans_blood[i]=ABO[f1][i][c1];
for(int i=0;i<2;i++) ans_Rh[i]=Rh[f2][i][c2];
show(father,0);printf(" ");
print(ans_blood,ans_Rh);printf(" ");
show(child,0);
}
else if(child==0)
{
int f1,m1,f2,m2;
if(father>0) {f1=father-1;f2=0;}//0为+,1为-
else {f1=-1-father;f2=1;}
if(mother>0) {m1=mother-1;m2=0;}
else {m1=-1-mother;m2=1;}
for(int i=0;i<4;i++) ans_blood[i]=ABO[f1][m1][i];
for(int i=0;i<2;i++) ans_Rh[i]=Rh[f2][m2][i];
show(father,0);printf(" ");
show(mother,0);printf(" ");
print(ans_blood,ans_Rh);
}
if(k!=T-1)printf("\n");
}
}
read函数负责将输入的字符转化为定义的数码,print函数负责将两个可能性数组中的内容结合,输出未知的人的血型字符串,show函数将数码转化为字符串,同时通过type整数来实现括号,逗号的管理。当print中cnt计数整数为1时添加前括号,大于1即中间的情况时需要在前面加逗号。最后要注意后括号和换行符的加入