九度OJ 1035 找出直系亲属
来源:互联网 发布:移动端商城源码 编辑:程序博客网 时间:2024/05/20 16:14
- 题目描述:
- 如果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 2ABCCDEEFGFABE0 0
- 样例输出:
great-grandparent-
#include <stdio.h>#include <string.h>int child[30];int find(int x, int y){int flag = 0, res = -1;while(child[x] != -1){if(child[x] == y)break;x = child[x];res++;}if(child[x] == y){return res;}elsereturn -1;}int main(){freopen("Test.txt","r",stdin);int n, m;while(scanf("%d%d",&n,&m) != EOF){char buf[5];int i, j;for(i = 0; i < 30; i++)child[i] = -1;for(i = 0; i < n; i++){scanf("%s",buf);int tmp1 = buf[0] - 'A';if(buf[1] != '-'){int tmp2 = buf[1] - 'A';child[tmp2] = tmp1;}if(buf[2] != '-'){int tmp3 = buf[2] - 'A';child[tmp3] = tmp1;}}for(i = 0; i < m; i++){scanf("%s",buf);int tmp1 = buf[0] - 'A';int tmp2 = buf[1] - 'A';int ans1 = find(tmp1,tmp2);int ans2 = find(tmp2,tmp1);if(ans1 == -1 && ans2 == -1){printf("-\n");}else if(ans1 != -1){if(ans1 == 0)printf("parent\n");else{for(j = 0; j < ans1; j++){printf("great-");}printf("grandparent\n");}}else if(ans2 != -1){if(ans2 == 0)printf("child\n");else{for(j = 0; j < ans2; j++){printf("great-");}printf("grandchild\n");}}}}return 0;}
0 0
- 九度oj 题目1035:找出直系亲属
- 九度OJ 1035 找出直系亲属
- 九度OJ 1035找出直系亲属
- 九度OJ 题目1035:找出直系亲属
- 九度OJ题目1035:找出直系亲属
- 九度OJ-题目1035 找出直系亲属
- 【九度OJ】题目1035:找出直系亲属
- 九度OJ 1035:找出直系亲属(二叉树)
- 九度OJ 1035:找出直系亲属 (二叉树、递归)
- 九度[1035]-找出直系亲属
- 九度1035 -树 - 找出直系亲属
- 九度oj 题目1035:找出直系亲属 【ZJU2009考研机试题5】
- 九度1035:找出直系亲属<并查集>
- 题目1035:找出直系亲属
- 题目1035:找出直系亲属
- 题目1035:找出直系亲属
- 题目1035:找出直系亲属
- 题目1035:找出直系亲属
- 数字签名是什么?
- CentOS不启动桌面
- UVa 11987,Almost Union-Find,并查集
- Servlet(一)——刨根问底
- 图片滚动标签 html
- 九度OJ 1035 找出直系亲属
- 可输入的下拉框
- AJAX技术与应用浅析
- SSL 的工作原理
- json
- 简单工厂模式的UML类图与源码实现
- Your Ubuntu 12.04 is running in 2D mode,Many features will not be available.(on Ubuntu 12.04)解决方法
- 记一次解决jenkins持续构建,自动部署的问题
- hadoop怎样创建 mapreduce项目简易例子教程