码迷,mamicode.com
首页 > 其他好文 > 详细

03.ount the Digit

时间:2017-07-25 11:51:55      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:ber   i++   ams   func   using   his   ...   foreach   iter   

Take an integer n (n >= 0) and a digit d (0 <= d <= 9) as an integer. Square all numbers k (0 <= k <= n)between 0 and n. Count the numbers of digits d used in the writing of all the k**2. Call nb_dig (or nbDig or ...) the function taking n and d as parameters and returning this count.

#Examples:

n = 10, d = 1, the k*k are 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
We are using the digit 1 in 1, 16, 81, 100. The total count is then 4.

nb_dig(25, 1):
the numbers of interest are
1, 4, 9, 10, 11, 12, 13, 14, 19, 21 which squared are 1, 16, 81, 100, 121, 144, 169, 196, 361, 441
so there are 11 digits `1` for the squares of numbers between 0 and 25.

Note that 121 has twice the digit 1.


function nbDig(n, d) {
// your code
var arr=[];
var num=0;
for(var i=0;i<=n;i++){
arr.push(i*i);

}

arr=arr.join("").split("");
var arrs=arr.filter(function(x,index){
return x==d
})
return arrs.length;
}

测试:nbDig(5750, 0)   4700

最佳答案:

function nbDig(n, d) { var res=0; for (var g=0;g<=n;g++){ var square=(g*g+"").split(""); square.forEach((s)=>s==d?res++:null) }return res; }

03.ount the Digit

标签:ber   i++   ams   func   using   his   ...   foreach   iter   

原文地址:http://www.cnblogs.com/chengnanbei/p/7233156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!