// 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;
}
1035_找出直系親屬
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.