13 June 2018

Find Number of Grandchildren

To find the number of grandchildren, great 
grandchildren great-great grandchildren and so-on of a given person. In 
this program, the input is a table with two columns, 'Child' and 'Father'.
Then we enter a name whose number grandchildren, great grandchildren
etc is to be calculated. We do not have to count children. An example is:
Given a two dimensional array of strings like
<”luke”, “shaw”>
<”wayne”, “rooney”>
<”rooney”, “ronaldo”>
<”shaw”, “rooney”> 

sample input:   
   4

 <”luke”, “shaw”>
 <”wayne”, “rooney”>
 <”rooney”, “ronaldo”>
 <”shaw”, “rooney”>
 ronaldo
Sample Output:
 2

import java.util.*;
public class NewClass7 {
       int c=0;
      void print()
      {
          System.out.println(c);
      }
       void count(String f[],String k)
      { 
          for(int i=0;i<f.length;i++)
          {
              if(k.equals(f[i]))
                  c++;
          }
      }
      void check(String c[],String f[],String r)
      {  int k=0;
          for(int i=0;i<f.length;i++)
          {
              if(r.equals(f[i]))
              {   k=1;
                  count(f,c[i]);
              }
          }
      }
     
public static void main(String args[])

      {
          Scanner s=new Scanner(System.in);
          NewClass7 ob=new NewClass7();
          int n=s.nextInt();
          String c[]=new String[n];
          String f[]=new String[n];
          for(int i=0;i<n;i++)
          {
             
              c[i]=s.next();
              f[i]=s.next();
          }s.nextLine();
                  String r="";
                  r=s.nextLine();
          ob.check(c, f, r);
          ob.print();
      }
   
}

No comments:

Post a Comment