Last active
August 29, 2015 13:56
-
-
Save googya/9133330 to your computer and use it in GitHub Desktop.
计算一个数字数组中 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
def count_one(a) | |
return 0 if a <= 0 | |
( a % 10 ) == 1 ? 1 + count_one( a / 10 ) : count_one( a / 10 ) | |
end | |
(1..100).to_a.inject(0){|sum, a| sum += count_one(a) } |
Author
googya
commented
Mar 21, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment