uva115

25 篇文章 0 订阅
1 篇文章 0 订阅

题目翻译

#include <iostream>
#include <string.h>
#include <map>
#include <stdio.h>
using namespace std;

int parent[350];//记录每个名号的父亲名号
map<string,int> nametonum;//为每个名字编号
int num;//名号
string namelist[350];//把名号对应名字
string firstname,secondname;

bool in_tree()
{
    if(nametonum.find(firstname)==nametonum.end())
    {
        cout<<"no relation"<<endl;
        return false;
    }
    if(nametonum.find(secondname)==nametonum.end())
    {
        cout<<"no relation"<<endl;
        return false;
    }
    return true;
}
bool in_same_tree()
{
    int first=nametonum[firstname];
    int second=nametonum[secondname];
    while(parent[first])
        first=parent[first];
    while(parent[second])
        second=parent[second];
    if(second!=first)
    {
        cout<<"no relation"<<endl;
        return false;
    }
    else
        return true;
}
bool is_descendent()
{
    int first=nametonum[firstname];
    int second=nametonum[secondname];
    int f=0;
    while(parent[first]!=second && parent[first])
    {
        first=parent[first];
        f++;
    }
    if(parent[first]==0) return false;
    else
    {
        if(f==0) cout<<"child"<<endl;
        else if(f==1) cout<<"grand child"<<endl;
        else
        {
            for(int i=1; i<f; i++)
            cout<<"great ";
            cout<<"grand child"<<endl;
        }


        return true;
    }
}
bool is_ancestor()
{
    int first=nametonum[firstname];
    int second=nametonum[secondname];
    int s=0;
    while(parent[second]!=first && parent[second])
    {
        second=parent[second];
        s++;
    }
    if(parent[second]==0) return false;
    else
    {
        if(s==0) cout<<"parent"<<endl;
        else if(s==1) cout<<"grand parent"<<endl;
        else
        {
            for(int i=1; i<s; i++)
            cout<<"great ";
            cout<<"grand parent"<<endl;
        }
        return true;
    }
}
bool is_sibling()
{
    if(parent[nametonum[firstname]]==parent[nametonum[secondname]])
    {
        cout<<"sibling"<<endl;
        return true;
    }
    else
        return false;
}
bool is_cousin()
{
    int first=nametonum[firstname];
    int second=nametonum[secondname];
    int f1=0,s1=0;
    while(parent[first])
    {
        first=parent[first];
        f1++;
    }
    while(parent[second])
    {
        second=parent[second];
        s1++;
    }
    first=nametonum[firstname];
    second=nametonum[secondname];
    int f2=0,s2=0;
    if(f1>s1)
    {
        for(int i=0; i<f1-s1; i++)
        {
            first=parent[first];
            f2++;
        }
        f2++;
        s2++;
        while(parent[first]!=parent[second])
        {
            first=parent[first];
            second=parent[second];
            f2++;
            s2++;
        }
        cout<<s2-1<<" cousin removed "<<f2-s2<<endl;
    }
    else if(f1<s1)
    {
        for(int i=0; i<s1-f1; i++)
        {
            second=parent[second];
            s2++;
        }
        f2++;
        s2++;
        while(parent[first]!=parent[second])
        {
            first=parent[first];
            second=parent[second];
            f2++;
            s2++;
        }
        cout<<f2-1<<" cousin removed "<<s2-f2<<endl;
    }
    else
    {
        f2++;
        s2++;
        while(parent[first]!=parent[second])
        {
            first=parent[first];
            second=parent[second];
            f2++;
            s2++;
        }
        cout<<f2-1<<" cousin"<<endl;
    }
    return true;
}
int main()
{
   // freopen("debug\\in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
   // freopen("debug\\out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中
    while(cin>>firstname>>secondname)
    {
        if(firstname=="no.child" && secondname=="no.parent") break;
        if(nametonum.find(firstname)==nametonum.end())//是个新出现的名字
        {
            nametonum[firstname]=++num;
            namelist[num]=firstname;
        }
        if(nametonum.find(secondname)==nametonum.end())//是个新出现的名字
        {
            nametonum[secondname]=++num;
            namelist[num]=secondname;
        }
        parent[nametonum[firstname]]=nametonum[secondname];
    }
    while(cin>>firstname>>secondname)
    {
        if(firstname==secondname)
            cout<<"no relation"<<endl;
        if(!in_tree())
            continue;
        if(!in_same_tree())
            continue;
        if(is_ancestor())
            continue;
        if(is_descendent())
            continue;
        if(is_sibling())
            continue;
        if(is_cousin())
            continue;
    }
    return 0;
}

这题把great和grand弄混……找个半天……
后get新技能:先在project里面新建debug文件夹,建立in和out文本文件,把数据复制到in里面,结果就在out里面了,看//后的代码~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值