1061 - Consanguine Calculations

Every person's blood has 2 markers called ABO alleles. Each of the markers is represented by one of three letters: AB, or O. This gives six possible combinations of these alleles that a person can have, each of them resulting in a particular ABO blood type for that person.


CombinationABO Blood Type
AAA
ABAB
AOA
BBB
BOB
OOO


Likewise, every person has two alleles for the blood Rh factor, represented by the characters + and -. Someone who is ``Rh positive" or ``Rh+" has at least one + allele, but could have two. Someone who is ``Rh negative" always has two - alleles.

The blood type of a person is a combination of ABO blood type and Rh factor. The blood type is written by suffixing the ABO blood type with the + or - representing the Rh factor. Examples include A+AB-, and O-.

Blood types are inherited: each biological parent donates one ABO allele (randomly chosen from their two) and one Rh factor allele to their child. Therefore 2 ABO alleles and 2 Rh factor alleles of the parents determine the child's blood type. For example, if both parents of a child have blood type A-, then the child could have either type A- or type O- blood. A child of parents with blood types A+ and B+ could have any blood type.

In this problem, you will be given the blood type of either both parents or one parent and a child; you will then determine the (possibly empty) set of blood types that might characterize the child or the other parent.

Note: an uppercase letter ``Oh" is used in this problem to denote blood types, not a digit (zero).

Input 

The input consists of multiple test cases. Each test case is on a single line in the format: the blood type of one parent, the blood type of the other parent, and finally the blood type of the child, except that the blood type of one parent or the child will be replaced by a question mark. To improve readability, whitespace may be included anywhere on the line except inside a single blood type specification.

The last test case is followed by a line containing the letters EN, and D separated by whitespace.

Output 

For each test case in the input, print the case number (beginning with 1) and the blood type of the parents and the child. If no blood type for a parent is possible, print ``IMPOSSIBLE". If multiple blood types for parents or child are possible, print all possible values in a comma-separated list enclosed in curly braces. The order of the blood types inside the curly braces does not matter.

The sample output illustrates multiple output formats. Your output format should be similar.

Sample Input 

O+  O-  ? 
O+  ?  O-
AB-  AB+  ? 
AB+  ?  O+ 
E N D

Sample Output 

Case 1: O+ O- {O+, O-}
Case 2: O+ {A-, A+, B-, B+, O-, O+} O-
Case 3: AB- AB+ {A+, A-, B+, B-, AB+, AB-}
Case 4: AB+ IMPOSSIBLE O+











#include<stdio.h>
const int ABO[4][4][4]={1,0,0,1, 1,1,1,1, 1,1,1,0, 1,0,0,1,
						1,1,1,1, 0,1,0,1, 1,1,1,0, 0,1,0,1,
						1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,0,0,
						1,0,0,1, 0,1,0,1, 1,1,0,0, 0,0,0,1};
const int Rh[2][2][2]={1,1, 1,1, 1,1, 0,1};
char ch;
int cases,i,j,k,l,father,mother,child;
int ans_ABO[4],ans_Rh[2];

void out(int x,int type)
{
	if(type==0)
		printf(" ");
	else if(type==1)
		printf(" {");
	else
		printf(", ");

	if(x==1||x==-1)
		printf("A");
	if(x==2||x==-2)
		printf("B");
	if(x==3||x==-3)
		printf("AB");
	if(x==4||x==-4)
		printf("O");
	if(x>0)
		printf("+");
	else
		printf("-");
}

int get()
{
	int tmp;
	scanf("%c",&ch);
	while(ch!='A'&&ch!='B'&&ch!='O'&&ch!='E'&&ch!='N'&&ch!='D'&&ch!='?')
		scanf("%c",&ch);
	if(ch=='E'&&ch=='N'&&ch=='D')
		return 0;
	if(ch=='?')
		return 5;
	if(ch=='A')
		tmp=1;
	else if(ch=='B')
		tmp=2;
	else
		tmp=4;
	scanf("%c",&ch);
	if(ch=='+')
		return tmp;
	else if(ch=='-')
		return -tmp;
	else
		tmp=3;
	scanf("%c",&ch);
	if(ch=='+')
		return tmp;
	else
		return -tmp;
}

void write()
{
	int t;
	k=0;l=0;
	for(i=0;i<4;i++)
		k+=ans_ABO[i];
	for(i=0;i<2;i++)
		l+=ans_Rh[i];
	if(k*l==0)
		printf(" IMPOSSIBLE");
	if(k*l==1)
		for(i=0;i<4;i++)
			if(ans_ABO[i])
				for(j=0;j<2;j++)
					if(ans_Rh[j])
					{
						if(j==0)
							out(i+1,0);
						else
							out(-i-1,0);
					}
	if(k*l>1)
	{
		t=0;
		for(i=0;i<4;i++) if(ans_ABO[i])
			for(j=0;j<2;j++) if(ans_Rh[j])
			{
				t++;
				if(j==0)
					out(i+1,t);
				else
					out(-i-1,t);
			}
		printf("}");
	}
}

int main()
{
	while(1)
	{
		father=get();
		mother=get();
		child=get();
		if(father==0)
			break;
		if(father==5)
		{
			if(mother>0)
				k=mother-1;
			else
				k=-1-mother;
			if(child>0)
				l=child-1;
			else
				l=-1-child;
			for(i=0;i<4;i++)
				ans_ABO[i]=ABO[i][k][l];

			if(mother>0)
				k=0;
			else
				k=1;
			if(child>0)
				l=0;
			else
				l=1;
			for(i=0;i<2;i++)
				ans_Rh[i]=Rh[i][k][l];
		}

		if(mother==5)
		{
			if(father>0)
				k=father-1;
			else
				k=-1-father;
			if(child>0)
				l=child-1;
			else
				l=-1-child;
			for(i=0;i<4;i++)
				ans_ABO[i]=ABO[k][i][l];

			if(father>0)
				k=0;
			else
				k=1;
			if(child>0)
				l=0;
			else
				l=1;
			for(i=0;i<2;i++)
				ans_Rh[i]=Rh[k][i][l];
		}

		if(child==5)
		{
			if(father>0)
				k=father-1;
			else
				k=-1-father;
			if(mother>0)
				l=mother-1;
			else
				l=-1-mother;
			for(i=0;i<4;i++)
				ans_ABO[i]=ABO[k][l][i];

			if(father>0)
				k=0;
			else
				k=1;
			if(mother>0)
				l=0;
			else
				l=1;
			for(i=0;i<2;i++)
				ans_Rh[i]==Rh[k][l][i];
		}
		printf("Case %d:",++cases);
		if(father!=5)
			out(father,0);
		else
			write();
		if(mother!=5)
			out(mother,0);
		else
			write();
		if(child!=5)
			out(child,0);
		else
			write();
		printf("\n");
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值