1030血型判断Ⅱ

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值