日期:2014-05-20  浏览次数:20722 次

面试题。
Math.round(11.5)等於多少? Math.round(-11.5)等於多少? 

 Math.round(11.5)==12Math.round(-11.5)==-11round方法返回与参数最接近的长整数 
能解释解释吗?  

编程题: 用最有效率的方法算出2乘以8等於几? 
  有C背景的程序员特别喜欢问这种问题。 
  2 < < 3 
  为什么啊 


------解决方案--------------------
Math.round(x) = Math.floor(x+0.5)。

2<<3是移位运算,将2左移3为,即2*2^3=2*8。
------解决方案--------------------
Math.round这个可以直接参考文档,看看是按照什么计算的就明白了。

编程题: 用最有效率的方法算出2乘以8等於几? 
用位运算的效率比乘法的效率高的多,可以回忆一下计算机组成原来上的基本原理,看看乘法是如何处理的。
------解决方案--------------------
位移的效率是最高的。。
------解决方案--------------------

------解决方案--------------------
1.JAVA Api文档
Returns the closest int to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type int. In other words, the result is equal to the value of the expression: 

(int)Math.floor(a + 0.5f)
2.
2在机器里是10,2<<3表示左移三位,后面+3个0,就是10000就是16了