Property Inheritance in Family
Coding Problem Keys
Property Inheritance in Family
Problem Statement
The hierarchy of a family is given as the input. The first N lines contain the family hierarchy with each line containing the parent and the child names separated by a space. Each person in the family inherits the property only from his parents. (The value of the property of a parent is divided equally among his children).After the family hierarchy information, the names of two family members X and Y are also passed as the input. The value of property V inherited by the family member X is also given as the input.
The program must find and print the value of property inherited by the family member Y.
Note: If the value of the property of a parent cannot be divided by the number of children without leaving a remainder, then consider the floor value as the amount inherited by a single child. (As an example if the property worth is 10000 and number of children is 3, then the amount inherited by a single child is 3333).
Boundary Condition(s)
1 <= N <= 50Input Format
The first line contains the value of N.Output Format
The first line contains the value of property inherited by Y.Example Input/Output 1
Input
6Output
Explanation
Example Input/Output 2
Input
8Output
5000Max Execution Time Limit: 5000 millisecs
Solution
Programming Language: C Language
#include<stdio.h>
#include <stdlib.h>
int inherits(char a[][2][101],char *b,int n) {
int x=1,j=-1,k=0;
while (1){
for (int i=0;i<n;i++){
if (strcmp(&a[i][1],b)==0){
j=i;
break;
}
}
if (j==-1)
return x;
for (int i=0;i<n;i++)
k+=(strcmp(&a[i][0],&a[j][0])==0);
x*=k;
strcpy(b,&a[j][0]);
k=0;j=-1;
}
}
int main() {
int n,k=0,x;
scanf("%d",&n);
char a[n][2][101],b[101],c[101];
for (int i=0;i<n;i++)
scanf("%s %s ",a[i][0],a[i][1]);
scanf("%s %d %s ",b,&x,c);
x*=inherits(a,b,n);
x/=inherits(a,c,n);
printf("%d ",x);
}
// Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments