, the while loop runs till there is only one candidate left in the stack. The last candidate will not be examined as it will not enter the loop. But we will have either A or B as winner excluding the candidate in the stack (i.e. after while loop we will have pseudo winner). We need to compare this winner with the one left in the stack. Use a flag to discriminate between A and B, to keep track of who would be the last winner. Instead I have used one more excess comparison.
The problem is, in the content I have explained to pop off two candidates and compare, but in the code I am popping off only one, and keeping tracking track of last winner by popping in the if-else clauses. If we pop off two candidates and push the winner back to stack, we don't need these conditions.
Take the input like the first candidate who will be pushed into stack as the celebrity or take input there is no celebrity.
, the while loop runs till there is only one candidate left in the stack. The last candidate will not be examined as it will not enter the loop. But we will have either A or B as winner excluding the candidate in the stack (i.e. after while loop we will have pseudo winner). We need to compare this winner with the one left in the stack. Use a flag to discriminate between A and B, to keep track of who would be the last winner. Instead I have used one more excess comparison.
The problem is, in the content I have explained to pop off two candidates and compare, but in the code I am popping off only one, and keeping tracking track of last winner by popping in the if-else clauses. If we pop off two candidates and push the winner back to stack, we don't need these conditions.
Take the input like the first candidate who will be pushed into stack as the celebrity or take input there is no celebrity.
การแปล กรุณารอสักครู่..
