Question Link
Question:Returns n numbers and obtains the maximum number of comparisons between the maximum number and the second largest number.
Ideas:We can compare the two data pairs by using a binary tree. The maximum value of the two numbers is equivalent to that of the two parent nodes. Therefore, we can know that the maximum value is n-1, then, assuming that all the left sons are greater than the right sons, the right son with the maximum value has the second largest number compared with the value in the left subtree, and the number of comparisons is the tree height.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> using namespace STD; int N; int main () {While (scanf ("% d", & N )! = EOF) {int ans = n-1 + (INT) (Ceil (log (N)/log (2)-1; printf ("% d \ n ", ans);} return 0 ;}