Advertisement
Guest User

Untitled

a guest
May 31st, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1.  
  2. In this program you are given a series of births, indicating the name and gender of the child. You are then given
  3. a name of someone who wants to become the new monarch of England. You are to output the number of people who
  4. have to die for that person to inherit the throne of england.
  5. Input
  6. The first line of the input will contain a positive integer b representing the number of births that the program will
  7. contain. The next b lines will each document a single birth chronologically. Each birth will of the form parent's
  8. name, child's name, and sex (where sex is a single letter either f or m), each separated by a single space. The names
  9. will contain no spaces. The first parent mentioned will be considered the reigning monarch.
  10. This will be followed by a positive integer q representing the number of queries to be made. Each of the next q
  11. lines will contain the name of a single royal.
  12.  
  13.  
  14. Output
  15. For each query name q, print on 1 line the number of people who need to die for the qth name to become the ruler.
  16. Sample Input
  17. 16
  18. elizabethII charles m
  19. elizabethII anne f
  20. elizabethII andrew m
  21. elizabethII edward m
  22. charles william m
  23. anne peter m
  24. anne zara f
  25. charles harry m
  26. andrew beatrice f
  27. andrew eugenie f
  28. edward louise f
  29. edward severn m
  30. william george m
  31. peter savannah f
  32. peter isla f
  33. zara mia f
  34. 2
  35. george
  36. peter
  37. Corresponding Sample Output
  38. 3
  39. 12
  40.  
  41.  
  42.  
  43. struct Person {
  44. string parent;
  45. string name;
  46. char sex;
  47. int numDescendants;
  48. Person (string nam ="", char sx = "m", string par ="");// sets the name,sex, and the parent's name
  49. };
  50.  
  51.  
  52.  
  53. class Royals {
  54. Person family[1000];
  55. int numMembers; // holds the current number of people in the royal family
  56. void upDateDecents(string name);// updates the number of decedents of a person just added, called from add
  57. public :
  58. Royals(); // constructor
  59. add(Person person); // adds a person to the array, updates the number of Members and the ancestors list,
  60. int howMany2Die(string mName);// returns the number of people who must die, so mName can become the monarch
  61. };
  62.  
  63.  
  64.  
  65. Algorithm
  66. The number of people that have to die (if you are the current monarch) is 0 The number of people that have to die if
  67. you are a male is The number of number of people your parent needs to kill to inherit + the number of male siblings
  68. and their descendants born before you The number of people that have to die if you are a female is The number of
  69. number of people your parent needs to kill to inherit + the number of male siblings + the number of female siblings
  70. who were born before you + all of those siblings descendants
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement