1035_找出直系亲属
2017-02-05 22:06
381 查看
// 1035_找出直系亲属.cpp : 定义控制台应用程序的入口点。 //题目1035:找出直系亲属 //时间限制:1 秒内存限制:32 兆特殊判题:否提交:2647解决:1054 //题目描述: //如果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 //- //来源: //2009年浙江大学计算机及软件工程研究生机试真题 #include "stdafx.h" #include "stdio.h" #include "iostream" #include "string.h" #include "string" using namespace std; int arr[1000]; void find(int a,int b){ int flag = 0; if(!flag){ int count = 0; int temp_a = a; while(arr[temp_a]!=-1){ count++; if(arr[temp_a]!=b){ temp_a = arr[temp_a]; } else{ if(count>1){ for(int i = 2;i<count;i++) cout<<"great-"; cout<<"grandparent"<<endl; } else cout<<"parent"<<endl; flag = 1; break; } } } if(!flag){ int count = 0; int temp_b = b; while(arr[temp_b]!=-1){ count++; if(arr[temp_b]!=a){ temp_b = arr[temp_b]; } else{ if(count>1){ for(int i = 2;i<count;i++) cout<<"great-"; cout<<"grandchild"<<endl; } else cout<<"child"<<endl; flag = 1; break; } } } if(!flag) cout<<"-"<<endl; } int main() { int n,m; while(cin>>n>>m && (m||n)){ string a; memset(arr,-1,sizeof(arr)); while(n--){ cin>>a; if(a[1]!='-') arr[a[1]-'A'] = a[0]-'A'; if(a[2]!='-') arr[a[2]-'A'] = a[0]-'A'; } while(m--){ cin>>a; find(a[0]-'A',a[1]-'A'); } } return 0; }
相关文章推荐
- 九度OJ 1035:找出直系亲属 (二叉树、递归)
- 九度OJ 1035:找出直系亲属 (二叉树、递归)
- OJ_1035 找出直系亲属
- 题目1035:找出直系亲属
- 题目1035:找出直系亲属(树的深度)
- 九度1035 -树 - 找出直系亲属
- 九度OJ 1035找出直系亲属
- 九度OJ题目1035:找出直系亲属
- [Jobdu] 题目1035:找出直系亲属
- 九度OJ题目1035:找出直系亲属
- 题目1035:找出直系亲属
- 题目1035:找出直系亲属
- 题目1035:找出直系亲属
- 九度oj 题目1035:找出直系亲属 【ZJU2009考研机试题5】
- 题目1035:找出直系亲属
- 九度OJ 1035 找出直系亲属
- 题目1035:找出直系亲属
- 九度[1035]-找出直系亲属
- 题目1035:找出直系亲属
- 题目1035:找出直系亲属