V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
ebeb
V2EX  ›  程序员

计算亲戚关系,有没有优雅的思路

  •  1
     
  •   ebeb · Dec 30, 2016 · 2009 views
    This topic created in 3404 days ago, the information mentioned may be changed or developed.

    计算亲戚关系,我的 XXX 的 XXX [的 XXX ] 是 YYY。如,我的爸爸的妈妈是奶奶。

    我的想法是这样: XXX 限定在哥哥、姐姐、弟弟、妹妹、爸爸、妈妈、丈夫、妻子、儿子、女儿。 YYY 是最后计算的结果,比如姑姑、姑奶奶等。

    目前我建立的模型是这样:

    class Person {
        string current_title;		//当前称呼
        Person compare;			//用于比较大小的对象,比如爸爸的弟弟可能是“伯父”、“叔公”
        //下面十个分别代表哥哥、弟弟、姐姐、妹妹、爸爸、妈妈、丈夫、妻子、儿子、女儿
        string elder_brother;
        string litte_brother;
        string elder_sister;
        string litte_sister;
        string father;
        string mother;
        string husband;
        string wife;
        string son;
        string daughter;
    }
    

    根据当前的current_title不同,后边十个属性分别取不同的称呼。

    这样的模型,可以用,但是总感觉这种方法很傻。

    求教,这个问题有没有优雅的解决思路。

    3 replies    2022-03-02 17:11:09 +08:00
    285571052
        1
    285571052  
       Dec 31, 2016
    有个想法,但是还是很麻烦的样子。
    假设有 哥哥、弟弟、姐姐、妹妹、爸爸、妈妈、丈夫、妻子、儿子、女儿 这些实例对象,两两对象之间的关系是明确的,然后用一个指针指向最终的对象,比如 我哥哥的姐姐的妈妈的女儿 这样,最终指针指向姐姐,然后判断姐姐和'我'的关系。
    mumuy
        2
    mumuy  
       Mar 2, 2022
    mumuy
        3
    mumuy  
       Mar 2, 2022
    我写的一套亲戚关系的算法: https://github.com/mumuy/relationship/
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4495 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 04:08 · PVG 12:08 · LAX 21:08 · JFK 00:08
    ♥ Do have faith in what you're doing.