有多少种方法在那里看到,如果一个数是偶数,哪一个是最快和最清晰?偶数、有多少、在那、种方法

由网友(蹲在街角等幸福)分享简介:给出任何数字,什么是判断它的即使是最好的方法是什么?你有多少方法能想到的,什么是最快的方法和清晰的方式?解决方案 布尔ISEVEN =((数量和放大器; 0×01)== 0)现在的问题说:任何号码,所以人们既可以丢弃的花车或第一扩展它们备份到一个完整的价值首先他们以另一种方式处理,也许是 - 看出来溢出 - 即改变...

给出任何数字,什么是判断它的即使是最好的方法是什么?你有多少方法能想到的,什么是最快的方法和清晰的方式?

解决方案

 布尔ISEVEN =((数量和放大器; 0×01)== 0)
 

现在的问题说:任何号码,所以人们既可以丢弃的花车或第一扩展它们备份到一个完整的价值首先他们以另一种方式处理,也许是 - 看出来溢出 - 即改变2.1〜21(乘以10并转换为int),然后测试。这可能是合理不过的假设,即通过提及任何数量的谁提出的问题的人实际上是指整数值。

given any number, what's the best way to determine it is even? how many methods can you think of, and what is the fastest way and clearest way?

解决方案 对一个自然数做如下操作 如果是偶数则除以2 如果是奇数,对一个自然数做如下

bool isEven = ((number & 0x01) == 0)

The question said "any number", so one could either discard floats or handle them in another manner, perhaps by first scaling them up to an integral value first - watching out for overflow - i.e. change 2.1 to 21 (multiply by 10 and convert to int) and then test. It may be reasonable to assume, however, that by mentioning "any number" the person who posed the question is actually referring to integral values.

阅读全文

相关推荐

最新文章