标签:des style blog color io ar strong for div
1 秒
内存限制:32 兆
特殊判题:否
提交:1892
解决:764
3 2 ABC CDE EFG FA BE 0 0
great-grandparent -
代码晚些补:
#include<stdio.h> using namespace std; int tree[26]; char buf[4]; int findd(int a,int b) {//查找a和b的关系,用双亲表示法 int cnt = 1; int flag = 1;//1表示a和b是在同一个族谱上,0表示没有关系 while(b != tree[a]) { if(a == tree[a]) {//已经到了根节点,a和b没有关系 flag = 0; break; } a = tree[a];//a的父节点(找孩子) cnt ++; } if(flag == 0) { return 0; }else { return cnt; } } int main(void) { int n,m; while(scanf("%d%d",&n,&m) != EOF ) { if(n == 0 && m == 0) { break; } //初始化 for(int i=0; i<26; ++i) { tree[i] = i; } while(n--) { scanf("%s",buf); int a = buf[0] - ‘A‘; tree[buf[1]-‘A‘] = a; tree[buf[2]-‘A‘] = a;//设定父节点 } while(m--) { scanf("%s",buf); int x = buf[0] - ‘A‘; int y = buf[1] - ‘A‘; int z = findd(x,y) - findd(y,x);//必有一个为0,通过两个相减正负判断子还是双亲 if(z == 0) {//说明没有关系 printf("-\n"); } else if(z == 1) { printf("parent\n"); } else if(z == 2) { printf("grandparent\n"); } else if(z > 2) { while(z-2) { printf("great-"); z--; } printf("grandparent\n"); }else if(z == -1) { printf("child\n"); } else if(z == -2) { printf("grandchild\n"); } else { while((-z)-2) { z ++; printf("great-"); } printf("grandchild\n"); } } } return 0; }
标签:des style blog color io ar strong for div
原文地址:http://www.cnblogs.com/sairre/p/3956671.html