zoukankan      html  css  js  c++  java
  • 2009年浙大 :找出直系亲属

    题目描述:
        如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外)祖父,祖母,则A,B是C的grandparent,C是A,B的grandchild,如果A,B是C的(外)曾祖父,曾祖母,则A,B是C的great-grandparent,C是A,B的great-grandchild,之后再多一辈,则在关系上加一个great-。
    输入:
        输入包含多组测试用例,每组用例首先包含2个整数n(0<=n<=26)和m(0<m<50), 分别表示有n个亲属关系和m个问题, 然后接下来是n行的形式如ABC的字符串,表示A的父母亲分别是B和C,如果A的父母亲信息不全,则用-代替,例如A-C,再然后是m行形式如FA的字符串,表示询问F和A的关系。
        当n和m为0时结束输入。
    输出:
        如果询问的2个人是直系亲属,请按题目描述输出2者的关系,如果没有直系关系,请输出-。
        具体含义和输出格式参见样例.
    样例输入:
    3 2
    ABC
    CDE
    EFG
    FA
    BE
    0 0
    样例输出:
    great-grandparent
    -
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int MAXN=105;
    int son[MAXN];
    int dep[MAXN];
    int main()
    {
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            if(n==0&&m==0)
                break;
            memset(son,-1,sizeof(son));
            memset(dep,0,sizeof(dep));
            for(int i=0;i<n;i++)
            {
                scanf("%*c");
                char a,b,c;
                scanf("%c%c%c",&a,&b,&c);
                a-='A';
                if(b!='-')
                {
                    b-='A';
                    son[b]=a;
                    dep[b]=dep[a]+1;
                }
                if(c!='-')
                {
                    c-='A';
                    son[c]=a;
                    dep[c]=dep[a]+1;
                }
            }
            while(m--)
            {
                scanf("%*c");
                char a,b;
                scanf("%c%c",&a,&b);
                a-='A';
                b-='A';
                int l=0;
                int start,goal;
                int up;
                if(dep[a]>dep[b])
                {
                    up=true;
                    start=a;
                    goal=b;
                }
                else
                {
                    up=false;
                    start=b;
                    goal=a;
                }
                int tmp=start;
                bool mark=false;
                while(son[tmp]!=-1)
                {
                    tmp=son[tmp];
                    l++;
                    if(tmp==goal)
                    {
                        mark=true;
                        break;
                    }
                }
                if(mark==false)
                {
                    printf("-
    ");
                }
                else
                {
                    if(up==true)
                    {
                        if(l==1)
                        {
                            printf("parent
    ");
                        }
                        else if(l==2)
                        {
                            printf("grandparent
    ");
                        }
                        else
                        {
                            for(int i=l;i>2;i--)
                            {
                                printf("great-");
                            }
                            printf("grandparent
    ");
                        }
                    }
                    else
                    {
                        if(l==1)
                        {
                            printf("child
    ");
                        }
                        else if(l==2)
                        {
                            printf("grandchild
    ");
                        }
                        else
                        {
                            for(int i=l;i>2;i--)
                            {
                                printf("great-");
                            }
                            printf("grandchild
    ");
                        }
                    }
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    caffe:使用C++来提取任意一张图片的特征(从内存读取数据)
    python:控制鼠标和键盘
    .dll 文件编写和使用
    python:打包成exe程序
    python:小乌龟turtle
    python:input()和raw_input()
    C++:哈希
    C++:线程(std::thread)
    GitHub:Git的使用
    链表
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5402372.html
Copyright © 2011-2022 走看看