Skip to content

Commit dd01e50

Browse files
author
uuk020
committed
循环数组中奇数幂运算cube_odd
1 parent e65bb1a commit dd01e50

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

cube_odd/cube_odd.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/*
3+
* 循环数组中奇数幂运算
4+
* 1.Find the sum of the odd numbers within an array, after cubing the initial integers.(注: 数组奇数三次幂相加)
5+
* This function will return undefined (NULL in PHP) if any of the values aren't numbers.
6+
*/
7+
function cube_odd(array $a)
8+
{
9+
$sum = 0;
10+
// 循环数组, 排除非数字的.
11+
foreach ($a as $v) {
12+
if (!is_numeric($v)) {
13+
return null;
14+
} elseif (0 != $v % 2) { // 筛选出奇数
15+
$sum += pow($v, 3);
16+
}
17+
}
18+
return $sum;
19+
}

0 commit comments

Comments
 (0)