博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3786 hdoj 3786
阅读量:4121 次
发布时间:2019-05-25

本文共 1567 字,大约阅读时间需要 5 分钟。

找出直系亲属

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 406    Accepted Submission(s): 168
Problem Description
如果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-。
 
Input
输入包含多组测试用例,每组用例首先包含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时结束输入。
 
Output
如果询问的2个人是直系亲属,请按题目描述输出2者的关系,如果没有直系关系,请输出-。
具体含义和输出格式参见样例.
 
Sample Input
3 2ABCCDEEFGFABE0 0
 
Sample Output
great-grandparent-
 
#include<stdio.h>
int main(){
    int rel[26];
    int m,n,i,j,temp,found,k;
    char inp[3];
    while(scanf("%d%d",&n,&m),m||n){
        for(i=0;i<26;i++){
            rel[i]=-1;
        }
        for(i=0;i<n;i++){
            scanf("%s",inp);
            if(inp[1]>='A'&&inp[1]<='Z'){
                rel[inp[1]-'A']=inp[0]-'A';
            }    
            if(inp[2]>='A'&&inp[2]<='Z'){
                                rel[inp[2]-'A']=inp[0]-'A';
                        }
        }
        for(i=0;i<m;i++){
            found=0;
            scanf("%s",inp);
            temp=inp[0]-'A';
            k=0;
            while(rel[temp]!=-1){
                k++;
                temp=rel[temp];
                if(temp==inp[1]-'A'){
                    found=1;
                    break;        
                }    
            }
            if(found){
                if(k==1){
                    printf("parent\n");
                }
                if(k==2){
                    printf("grandparent\n");
                }
                if(k>2){
                    for(j=0;j<k-2;j++){
                        printf("great-");
                    }
                    printf("grandparent\n");
                }
                continue;
            }
            temp=inp[1]-'A';
            k=0;
            while(rel[temp]!=-1){
                                k++;    
                                temp=rel[temp];
                                if(temp==inp[0]-'A'){
                                        found=1;
                                        break;
                                }
                        }
                        if(found){
                                if(k==1){
                                        printf("child\n");
                                }
                                if(k==2){
                                        printf("grandchild\n");
                                }
                                if(k>2){
                                        for(j=0;j<k-2;j++){
                                                printf("great-");
                                        }
                                        printf("grandchild\n");
                                }
                                continue;
                        }
            printf("-\n");
        }
    }
    return 0;
}

转载地址:http://sxtpi.baihongyu.com/

你可能感兴趣的文章
iWatch报错: Authorization request cancled
查看>>
iWatch报错: Authorizationsession time out
查看>>
如何运行从网上下载的iWatch项目详细步骤.
查看>>
X-code7 beta error: warning: Is a directory
查看>>
Error: An App ID with identifier "*****" is not avaliable. Please enter a different string.
查看>>
X-code beta 开发iWatch项目,运行没有错误,但是某些操作一点就崩,而且找不错误的原因场景一
查看>>
Xcode 报错: Extra argument in call
查看>>
iTunes Connect 上传APP报错: Communication error. please use diagnostic mode to check connectivity.
查看>>
#import <Cocoa/Cocoa.h> 报错 Lexical or Preprocessor Issue 'Cocoa/Cocoa.h' file not found
查看>>
`MQTTClient (~> 0.2.6)` required by `Podfile`
查看>>
X-Code 报错 ld: library not found for -lAFNetworking
查看>>
Bitcode
查看>>
If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
查看>>
3.5 YOLO9000: Better,Faster,Stronger(YOLO9000:更好,更快,更强)
查看>>
iOS菜鸟学习--如何避免两个按钮同时响应
查看>>
How to access the keys in dictionary in object-c
查看>>
iOS菜鸟学习—— NSSortDescriptor的使用
查看>>
hdu 3787 hdoj 3787
查看>>
hdu 3790 hdoj 3790
查看>>
hdu 3789 hdoj 3789
查看>>