Fortune Telling Collection - Comprehensive fortune-telling - Logical thinking process of developing relatives and friends calculator

Logical thinking process of developing relatives and friends calculator

Due to business needs, it is necessary to realize the function of calculating relatives and friends.

1. Direct match.

When I saw the demand, I immediately thought of going online to see if there was a similar demo. Because I developed Android, I didn't find the corresponding source code, so I thought about writing it myself. I found a demo implemented by js on Github, and I simply understood the realization principle: get the numerical values that need to be calculated, make corresponding combinations, such as my brother's brother, and then directly match the combination results with the expressions I wrote. Will be directly combined into? Dad's dad? And match dad's dad. Is it grandpa? In this way, and then get the result. However, there are at least thousands of such combinations, and the performance is very poor. I want to write it myself, which is obviously cumbersome.

2 .? distributed computing

My first thought in dealing with this relationship is to use two combined calculation methods, that is, to calculate the past step by step. Like what? The wife of dad's brother's son? I count my father's brother as my uncle, my uncle's son as my cousin, and then my wife as my cousin or sister-in-law. But I can't match it with words, or it will be too huge. I think everyone needs a number to correspond, that is, I represent 0, my father represents 1, and my mother represents 2 ... all of them? Relatives are replaced by a number. I am also very clever, using the singular number of the man and the even number of the wife, so that+1 and-1 can be directly used to calculate a person's wife or husband. Of course, these are just ideas. If we continue to think according to this logical relationship, we will find that it is not easier than the first way. Because there are several calculation methods for each relationship, several calculations are needed for each representative's numerical value, that is, for 1 (father), I need to work out the relationships between his brothers and sisters, parents, children and myself, and 2 (mother) needs to list all the situations. As complicated as the first case, there will be many imperfections. For example, the wife of a father and son has three answers? Sister-in-law, sister-in-law and wife. If it is further, what about the wife of Dad's son? Sister-in-law, sister-in-law, wife and cousin. This way is completely impossible.

3. Direct calculation.

Grandpa's granddaughter has a sister-in-law, a sister-in-law, a wife, a cousin, and my father's daughter-in-law has a sister-in-law and a wife. Seeing this, I suddenly had a new idea. For a person or an identity, his descendants have the same name. My brother's next generation must be my nephew. The man is a nephew and the woman is a niece. My sister's is a son. Children, husbands, wives, and your grandfather or uncle are all connected with you because of their relationship with their parents. So you must have a line to connect, so I just need to know the highest point of this line and the direct gap between this person and me that needs to be calculated. For example, what does the highest point of this line mean? Dad's brother's son? The highest point is dad's brother, that is, uncle. Dad's dad's son? The highest point is dad's dad, that is, grandpa? That is, the person with the highest qualifications who needs to be calculated, the gap between us is you? Which generation is it? I define my generation as generation 0, father, mother, uncle, uncle ... as generation 1, grandfather generation 2, son, nephew, cousin generation-1 ... father brother's son? At the height, my father's brother was an uncle, and my son and I were the same generation. That must be my cousin. My cousin lost his father's son. Is the highest point grandpa? His son's generation is 1, which is your father's generation, so it's just dad, uncle, uncle. Just define two variables, one is to record the generation of the current person, the parents will be+1, and the children will be-1. Another variable will record the highest point, and the result is to calculate his descendants according to the highest point. It should be noted that the numerical value needs to be integrated before calculation. Is the brother of the brother directly integrated into the brother? The father of xxx's son must be xxx(xxx is a male) or xxx's husband (xxx is a female) ... It is necessary to integrate all temptations or this unnecessary. When calculating, should you pay attention to anything other than your spouse or yours? Brothers and sisters of immediate family members are related to you, and brothers and sisters of other spouses are not related to you? Just as your sister-in-law's brothers and sisters have nothing to do with you, you need to make a good judgment in this respect. Grandpa's grandson must be your father's child? Descendants of the father's brother. Also note that the highest point is grandpa, who is the same generation as you, but it may be the descendant of your grandpa's son or your grandpa's daughter. When these are handled, it's almost done.

It's a little simple, just a logical thinking process. This is just an idea.