Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // First iteration through n guest list to populate
- // our initial celebrity list
- Populate celebrity list:
- for(int i = 1 ; Not at the end of guest list ; i++) {
- Ask guest[i=1] if they know guest[i] // ask if A knows B
- if (Answer == yes) {
- // Since A knows of B then A cannot be a celebrity by definition
- // However now B is a potential celebrity
- Add guest[i] onto celebrity list
- remove guest[i-1] from celebrity list if on it
- }
- else // answer is no
- Add guest[i-1] onto celebrity list
- remove guest[i] from celebrity list if on it
- }
- // by the end of the population of the potential celebrity list
- // we are guaranteed to have the celebrity in it. Next it is time to empty
- // the celebrity list to find the actual celebrity
- int j = 1
- while(Contains more than 1 person)
- Ask celebrity[j-1] if they know celebrity[j]
- if (Answer == yes)
- remove celebrity[j-1] from the list
- else
- // we remove celebrity[j] from the list based off of
- // the definition that celebrities are known by everyone
- remove celebrity[j] from the list
- // if statement to ensure that we don't go to an infinite loop
- // when the real celebrity is index 0 or if there are no celebrities at all
- if (number of celebrities in the celebrity list is == 2)
- Ask celebrity[j] if they know celebrity[i]
- // repeat same code as above
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement