zoukankan      html  css  js  c++  java
  • You are my brother

    You are my brother

    时间限制: 1 Sec 内存限制: 128 MB


    题目描述

    Little A gets to know a new friend, Little B, recently. One day, they realize that they are family 500 years ago. Now, Little A wants to know whether Little B is his elder, younger or brother.

    输入

    There are multiple test cases.
    For each test case, the first line has a single integer, n (n<=1000). The next n lines have two integers a and b (1<=a,b<=2000) each, indicating b is the father of a. One person has exactly one father, of course. Little A is numbered 1 and Little B is numbered 2.
    Proceed to the end of file.

    输出

    For each test case, if Little B is Little A’s younger, print “You are my younger”. Otherwise, if Little B is Little A’s elder, print “You are my elder”. Otherwise, print “You are my brother”. The output for each test case occupied exactly one line.

    样例输入

    5
    1 3
    2 4
    3 5
    4 6
    5 6
    6
    1 3
    2 4
    3 5
    4 6
    5 7
    6 7

    样例输出

    You are my elder
    You are my brother

    题意概括

    给出数字之间的关系,问1和2的辈分关系;

    解题思路

    根据所给数据,搜索计算出1和2分别到祖先的距离,根据到祖先的距离然后判断出1和2的辈分关系、

    代码

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<ctype.h>
    #include<algorithm>
    using namespace std;
    
    int line[2010][2010];
    int dfs(int x,int num)
    {
        int i,j;
        num++;
        for(i=0;i<=2000;i++){
            if(line[x][i]){
                return dfs(i,num);
            }
        }
        return num;
    }
    int main()
    {
        int n;
        int i,j,a,b;
        while(scanf("%d",&n)!=EOF)
        {
            memset(line,0,sizeof(line));
            for(i=0;i<n;i++){
                scanf("%d %d",&a,&b);
                line[a][b]=1;
            }
            a=dfs(1,0);
            b=dfs(2,0);
            if(a<b){
                printf("You are my younger
    ");
            }else if(a==b){
                printf("You are my brother
    ");
            }else{
                printf("You are my elder
    ");
            }
        }
        return 0;
    }
    
  • 相关阅读:
    [718. 最长重复子数组]
    排序算法--归并,堆,快速排序
    改进的插排--希尔排序
    排序算法--选泡插
    对封装继承多态的理解
    Servlet[springmvc]的Servlet.init()引发异常
    [面试题 16.18. 模式匹配]
    [124. 二叉树中的最大路径和](
    7.29_python_lx_day11
    7.28_python_lx_day18
  • 原文地址:https://www.cnblogs.com/lanaiwanqi/p/10445723.html
Copyright © 2011-2022 走看看