03.ount the Digit

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; }

时间: 2024-08-26 23:54:49

03.ount the Digit的相关文章

The US ASCII Character Set 对应码 可以解决 URL中的特殊符号的传输问题

The US ASCII Character Set US ASCII, ANSI X3.4-1986 (ISO 646 International Reference Version) Codes 0 through 31 and 127 (decimal) are unprintable control characters.Code 32 (decimal) is a nonprinting spacing character. Codes 33 through 126 (decimal)

&lt;hdu - 1600 - 1601&gt; Leftmost Digit &amp;&amp; Rightmost Digit 数学方法求取大位数单位数字

1060 - Leftmost Digit 1601 - Rightmost Digit 1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * 10^x;其中x也是未知的: 两边取log10有:lg(n^n) = lg(a * 10^x); 即:n * lg(n)  - x = lg(a); 现在就剩x一个变量了,我们知道x是值n^n的位数-1,a向下取整就是我们要求的数: 所以 按着上面的推导式翻译成代码就可以了(注意:数值的范围和之间的

HDU 1060 Leftmost Digit (数论)

Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13680    Accepted Submission(s): 5239 Problem Description Given a positive integer N, you should output the leftmost digit of N^N.

03 php 数据类型:整数,进制转换,浮点,字符,布尔,数组,空类型,类型转换,算术运算,比较运算

03 数据类型:整数,进制转换,浮点,字符,布尔,数组,空类型,类型转换, 算术运算,比较运算,逻辑运算,短路现象, 三目运算符,字符型运算: 数据类型 整体划分 标量类型: int, float, string, bool 复合类型: array,     object 特殊类型: null,     resouce 整数类型int, integer 3种整数表示法 十进制写法:123: $n1 = 123; 八进制写法: 0123 $n2 = 0123; 十六进制写法: 0x123 $n3

前端开发神器WebStorm--Grunt 搭建环境(03)

通过上一篇前端开发神器WebStorm--自动化工作流(前言),相信大家都Grunt自动化工具有了初步了解. 接下来我就以WROC3000 web为原型,演示一下如何使用Grunt工具提高工作效率,最大程度压缩代码. 1.首先安装node环境 进入官网 下载安装.(记住安装目录) 检测安装成功方法:打开CMD窗口,输入 node --version 会打印出安装的版本号,说明已经安装成功. 2.安装 Grunt 客户端 在CMD 窗口中,切换到node安装盘符,会自动切换到nodejs安装目录.

MyBatis笔记03

1.动态sql 01.if:单独使用if,后面必须有where 1=1 代码:<!-- 需要注意的事项:01. 在xml文件中 特殊字符的使用 &&必须换成 and或者 & < < > > <= <= >= >= ' &apos;" " 02.因为不确定用户输入的到底是哪个参数 所以 where 之后必须加上 1=1 而且 每个条件之前加上 and --> <select id="

Environmental.Science.Limited.ChemHELP.v2.03

Environmental.Science.Limited.ChemHELP.v2.03 VMGSIM.V9.0.46最新版流程模拟软件     chemhelp易于安装和设置(系统要求如下).在一台电脑上,化学制品可以从数据库中选择,输入他 们的全名,或者使用一个搜索,可以指定一个部分的化学名称,中国科学院或联合国的数字,欧共体或欧 共体指数,甚至是一个风险短语.可以为选定的化学物显示的数据包括索引编号.危险符号.芯片分类和 标签.风险短语.物理性质.同义词.以及更多.正如这表明,在数据库中的

LeetCode 233. Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 本来想凑一个关于阶乘的题目,做个笔记,不枉我昨天A八个

TOJ 2703: Cow Digit Game

2703: Cow Digit Game Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte Total Submit: 1            Accepted:1 Description Bessie is playing a number game against Farmer John, and she wants you to help her achieve victory. Game i start