牛客网浙江大学机试--找出直系亲属

本文深入探讨了使用数组和二叉树解决人际关系查询问题的两种算法实现。通过具体代码示例,详细讲解了如何利用这两种数据结构查找人物间的亲属关系,如父母、子女、祖父母等。适用于算法学习和数据结构复习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目: 牛客网链接

概述: 用三个字母表示出三个人的关系,并回答提出的问题。

思路: 这题采用两种方法:

  1. 采用数组解决问题
    数组用来存储子女的下标(反正是独生子女),找关系的时候就不断地搜索子女。这里分为两种情况,一种是第一个字母是第二个字母的长辈,另一个是第一个字母是第二个字母的晚辈。只要把参数顺序倒过来即可。

  2. 采用树的方法来解决问题。这里就没写粘贴的牛客网的代码。思路很清晰,很适合复习二叉树相关的知识。

数组:

#include <iostream>
#include <algorithm>
using namespace std;

int child[200];

int find_child(int a, int b, int count)
{

    if(child[a] == -1)
    {
    	return 0;
    }
    else if(child[a] == b)
    {
    	return count;
    }
    
    int temp = find_child(child[a], b, count+1);
    return temp; 
}


int main()
{
	int n, m;
	while(scanf("%d%d", &n, &m) != EOF)
	{
		
        fill(child, child+200, -1);
        
        //输入关系 
        char temp[4];
		for(int i = 0; i < n; i++)
		{
			scanf("%s", temp);
			if(temp[1] != '-') { child[(int)temp[1]] = (int)temp[0];}
			if(temp[2] != '-') { child[(int)temp[2]] = (int)temp[0];}
		}
		
		//输入查询的问题
		char ques[3];
		for(int i = 0; i < m; i++)
		{
			scanf("%s", ques);
			int flag = find_child(ques[0], ques[1], 1);
			if(flag)
			{
				if(flag == 1) printf("parent\n");
				else
				{
					for(int j = 2; j < flag; j++)
					{
						printf("great-");
					}
					printf("grandparent\n");
				}				
				continue;
			}
			flag = find_child(ques[1], ques[0], 1);
			if(flag)
			{
				if(flag == 1) printf("child\n");
				else
				{
					for(int j = 2; j < flag; j++)
					{
						printf("great-");
					}
					printf("grandchild\n");
				}			
				continue;
			}
			else
			{
				printf("-\n");
			}
		} 
			
	}
	
	return 0;
}

二叉树:

链接:https://www.nowcoder.com/questionTerminal/2c958d09d29f46798696f15ae7c9703b
来源:牛客网

#include <stdio.h>
#include <map>
#define N 26
using namespace std;
 
struct Node{//二叉树节点
    int p1;//第一个双亲的下标,-1表示不存在
    int p2;//第二个双亲的下标,-1表示不存在
}tree[N];//顺序存储,下标就是它所代表的字符编号,比如0代表'A'
 
int preOrder(int from, int to, int depth)
{//从from出发先序遍历到找到to为止,并返回to相对于from的深度
    if(from==to) return depth;
    if(tree[from].p1!=-1)
    {
        int ret=preOrder(tree[from].p1, to, depth+1);
        if(ret!=-1) return ret;
    }
    if(tree[from].p2!=-1)
    {
        int ret=preOrder(tree[from].p2, to, depth+1);
        if(ret!=-1) return ret;
    }
    return -1;
}
 
int main()
{
    int n, m;
    while(scanf("%d %d", &n, &m)!=EOF)
    {
        for(int i=0; i<N; i++)
        {
            tree[i].p1=tree[i].p2=-1;
        }
        while(n--)//构建树
        {
            char str[4];
            scanf("%s", str);
            if(str[1]!='-') tree[str[0]-'A'].p1=str[1]-'A';
            if(str[2]!='-') tree[str[0]-'A'].p2=str[2]-'A';
        }
        while(m--)//查询
        {
            char str[3];
            scanf("%s", str);
            int from=str[0]-'A';
            int to=str[1]-'A';
            int ans1=preOrder(from, to, 0);
            if(ans1==1) printf("child\n");
            else if(ans1>=2)
            {
                for(int i=ans1; i>2; i--) printf("great-");
                printf("grandchild\n");
            }
            else//看来不是晚辈,那就是长辈了
            {
                int ans2=preOrder(to, from, 0);
                 if(ans2==1) printf("parent\n");
                else if(ans2>=2)
                {
                    for(int i=ans2; i>2; i--) printf("great-");
                    printf("grandparent\n");
                }
                else printf("-\n");//也不是长辈,那就不是直系亲属
            }
        }
    }
    return 0;//大功告成
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值