Skip to content

Commit 2a5a641

Browse files
author
uuk020
committed
循环某个范围的数, 计算平方, 根据第二个参数数字, 计算该数字出现几次nbDig
1 parent dd01e50 commit 2a5a641

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

nbDig/nbDig.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/*
3+
* 循环某个范围的数,算出每个数的平方,根据第二个参数中数字,看该数字在得出结果出现几次
4+
*/
5+
function nbDig($n, $d)
6+
{
7+
// your code
8+
$str = '';
9+
for ($i = 0; $i <= $n; $i++) {
10+
// 计算数字后拼接字符串
11+
$str .= (string)pow($i, 2);
12+
}
13+
// substr_count计算字串出现的次数
14+
$count = substr_count($str, (string)$d);
15+
return $count;
16+
}
17+
/**
18+
* 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.
19+
* Count the numbers of digits d used in the writing of all the k**2. Call nb_dig (or nbDig or ...)
20+
* the function taking n and d as parameters and returning this count.
21+
* n = 10, d = 1, the k*k are 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
22+
* We are using the digit 1 in 1, 16, 81, 100. The total count is then 4.
23+
*/

0 commit comments

Comments
 (0)