首页 > 代码库 > 题目1035:找出直系亲属(树的深度)
题目1035:找出直系亲属(树的深度)
题目链接:http://ac.jobdu.com/problem.php?pid=1035
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
//// 1035 找出直系亲属.cpp// Jobdu//// Created by PengFei_Zheng on 30/04/2017.// Copyright © 2017 PengFei_Zheng. All rights reserved.// #include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>#include <cstring>#include <cmath>#define MAX_SIZE 30 //#define debugusing namespace std; int family[MAX_SIZE]; int n, m; int find(int a, int b){ int depth = 1; while(family[a]!=-1){ if(family[a] == b) return depth; a = family[a]; depth++; } return -1;} int main(){#ifdef debug freopen("/Users/pengfei_zheng/Desktop/input.txt", "r", stdin);#endif while(scanf("%d%d",&n,&m)!=EOF){ if(0==n && 0==m) break; memset(family,-1,sizeof(family)); char str[4]; for(int i = 0 ; i < n ; i++){ scanf("%s",str); family[str[1]-‘A‘] = family[str[2]-‘A‘]=str[0]-‘A‘; } char ask[2]; for(int i = 0 ; i < m ; i++){ scanf("%s",ask); int x = find(ask[0]-‘A‘,ask[1]-‘A‘); int y = find(ask[1]-‘A‘,ask[0]-‘A‘); int maxDepth = max(x,y); if(maxDepth<=0){ printf("-\n"); } else if(x>0){ if(maxDepth==1){ printf("parent\n"); } else{ for(int i = 2 ; i < maxDepth ; i++){ printf("great-"); } printf("grandparent\n"); } } else if(y>0){ if(maxDepth==1){ printf("child\n"); } else{ for(int i = 2 ; i < maxDepth ; i++){ printf("great-"); } printf("grandchild\n"); } } } } return 0;}/************************************************************** Problem: 1035 User: zpfbuaa Language: C++ Result: Accepted Time:0 ms Memory:1520 kb****************************************************************/
题目1035:找出直系亲属(树的深度)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。