一家人(模拟)

一家人

时间限制: 1 Sec  内存限制: 32 MB
提交: 10  解决: 7
[提交][状态][讨论版][命题人:外部导入][Edit] [TestData]

题目描述

最近小明交了一个新朋友叫小宇,他们在聊天的时候发现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

提示

#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;
}

猜你喜欢

转载自www.cnblogs.com/caiyishuai/p/9505023.html