大门|大门-CS-2007 |问题85

考虑下面的C代码段:

null
  int j, n;
  j = 1;
  while (j <= n)
        j = j*2; 

任何n>0的循环执行中进行的比较次数为:

在所有选项中,日志的基数为2。 (A) CEIL(logn)+2 (B) N (C) CEIL(logn) (D) 楼层(logn)+2 答复: (D) 说明:

We can see it by taking few examples like n = 1, n = 3, etc.

For example, for n=5 we have the following (4) comparisons:
------------------------
1 <= 5 (T)
2 <= 5 (T)
4 <= 5 (T)
8 <= 5 (F)

------------------------
FLOOR(log_2 n)+2 = FLOOR(log_2 5) + 2 = FLOOR(2.3) + 2 = 2 + 2 = 4

这个问题的小测验

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享