UVA11714 Blind Sorting【对数】

it2022-05-26  72

I am a polar bear. But I am not just an ordinary polar bear. Yes I am extra ordinary! I love to play with numbers. One day my very good friend Mr. Panda came to me, and challenged me to solve a puzzle. He blindfolded me, and said that I have n distinct numbers. What I can ask is whether a-th number is larger than b-th number and he will answer me properly. What I have to do is to find out the largest and second largest number. I thought for a while and said “Come on, I will do it in minimum number of comparison.” Input There will be a non-negative integer, n in each of the line of input where n is as described above. n will be less than any 10 digit prime number and not less than the smallest prime. Output For each n, output number of questions that I have to ask Mr. Panda in the worst case. Sample Input 2 4 Sample Output 1 4

问题链接:UVA11714 Blind Sorting 问题简述:(略) 问题分析:     从n个数中,找出最大的2个数,求其最少的比较次数?     考虑分治思想,先对所有数进行两两比较,然后淘汰1个数,如同构筑一个完全二叉树。但是需要考虑细节…。其他不解释。 程序说明:(略) 参考链接:(略) 题记:(略)

AC的C++语言程序如下:

/* UVA11714 Blind Sorting */ #include <bits/stdc++.h> using namespace std; int main() { int n; while(~scanf("%d", &n)) printf("%d\n", n + (int)ceil(log2(n)) - 2); return 0; }

最新回复(0)