Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. // First iteration through n guest list to populate
  2. // our initial celebrity list
  3. Populate celebrity list:
  4.     for(int i = 1 ; Not at the end of guest list ; i++) {
  5.         Ask guest[i=1] if they know guest[i] // ask if A knows B
  6.  
  7.         if (Answer == yes) {
  8.             // Since A knows of B then A cannot be a celebrity by definition
  9.             // However now B is a potential celebrity
  10.             Add guest[i] onto celebrity list
  11.             remove guest[i-1] from celebrity list if on it
  12.         }
  13.         else // answer is no
  14.             Add guest[i-1] onto celebrity list
  15.             remove guest[i] from celebrity list if on it
  16.     }
  17.  
  18. // by the end of the population of the potential celebrity list
  19. // we are guaranteed to have the celebrity in it. Next it is time to empty
  20. // the celebrity list to find the actual celebrity
  21. int j = 1
  22. while(Contains more than 1 person)
  23.     Ask celebrity[j-1] if they know celebrity[j]
  24.  
  25.     if (Answer == yes)
  26.         remove celebrity[j-1] from the list
  27.     else
  28.         // we remove celebrity[j] from the list based off of
  29.         // the definition that celebrities are known by everyone
  30.         remove celebrity[j] from the list
  31.  
  32.     // if statement to ensure that we don't go to an infinite loop
  33.     // when the real celebrity is index 0 or if there are no celebrities at all
  34.     if (number of celebrities in the celebrity list is == 2)
  35.         Ask celebrity[j] if they know celebrity[i]
  36.  
  37.         // repeat same code as above
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement