• 一家人(family)


    题目描述

    最近小明交了一个新朋友叫小宇,他们在聊天的时候发现500年前他们竟然是一家人!现在小明想知道小宇是他的长辈,晚辈,还是兄弟。

    输入

    输入包含多组测试数据。每组首先输入一个整数N(N<=10),接下来N行,每行输入两个整数a和b,表示a的父亲是b(1<=a,b<=20)。小明的编号为1,小宇的编号为2。
    输入数据保证每个人只有一个父亲。

    输出

    对于每组输入,如果小宇是小明的晚辈,则输出“You are my younger”,如果小宇是小明的长辈,则输出“You are my elder”,如果是同辈则输出“You are my brother”。

    样例输入

    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

    思路:
      这道题目的数据是a的b关系。(多组数据,这是个坑!)
    纯模拟。
    再按照判断即可。
    如果小宇是小明的晚辈,则输出“You are my younger”,
    如果小宇是小明的长辈,则输出“You are my elder”,
    如果是同辈则输出“You are my brother”。
      开两个数组存储a与b的状态比较。
    也可以用链表向上查找。
    代码:
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    int a[105];
    int main(){
        int n;
        while(cin>>n){
            for(int i=1;i<=100;i++)
                a[i]=i;
            for(int i=1;i<=n;i++){
                int x,y;
                cin>>x>>y;
                a[x]=y;
            }
            int s1=0;
            for(int i=1;i!=a[i];i=a[i])s1++;
            int s2=0;
            for(int i=2;i!=a[i];i=a[i])s2++;
            if(s1>s2)cout<<"You are my elder"<<endl;
            else if(s1==s2)cout<<"You are my brother"<<endl;
            else cout<<"You are my younger"<<endl;
        }
        return 0;
    }
    
    
    

      

    OVER!
     
  • 相关阅读:
    Luogu P1020 导弹拦截
    洛谷 p1196 带权并查集
    gradle 语法基础
    Codeforces Round #542 div.2 C
    我了解的字符编码
    洛谷p3374 树状数组1
    树状数组2
    线段树模板 求区间和, 区间加法,乘法更新
    洛谷 p1886 滑动窗口
    HDOJ.1251
  • 原文地址:https://www.cnblogs.com/wangshengjun/p/10628661.html
Copyright © 2020-2023  润新知