1035_找出直系親屬
2019-11-11 06:19:52
供稿:網友
// 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;}