Created
May 27, 2014 21:50
-
-
Save jikeytang/1c2f1b8990cb1d75e0ba to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140528-题目1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
输入任意数字,求该数二进制表达中有多少个1。 | |
例如:输入10,由于其二进制表示为1010,有两个1,因此输出2 | |
PS: | |
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。 | |
```javascript | |
// you code | |
``` | |
2. 粘贴代码时请使用shift+tab,缩进前面的空白。 |
wzc602003869
commented
May 28, 2014
// 经过昨天的那道题,我对我正则又没信心了 普通的吧
var get_num = function(num){
if(typeof num === 'number'){
return num.toString(2).replace(/0/g,'').length;
}
throw 'only number';
};
(454500).toString(2).match(/1/g).length
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment