从易到难,6个代表性编程问题,你敢挑战吗?附解答方案( 十 )


\n \n

4.  寻找不同的幂数

\n \n

通过蛮力的方法解决了这个问题 。  将每个结果添加到数组 , 然后从数组中删除重复项 。 最后一步是对数组进行排序 。

\n
  • \n
  • <?phpfunctiondistinctPowers($min $max) { $numbers = [
    ;  for ($i = $min; $i <= $max; $i++) {  for ($j = $min; $j <= $max; $j++){    $numbers[
    = pow($i $j);    $unique_numbers =array_unique($numbers); sort($unique_numbers);  return $unique_numbers;echo print_r(distinctPowers(2 5) 1); // [4 8 9 16 25 27 32 64 81125 243 256 625 1024 3125
    echo print_r(count(distinctPowers(2 100)) 1); // 9183 distinct terms

  • \n
    \n
\n \n

5.  Kaprekars Constant函数

\n \n

Kaprekars Constant函数问题有点难解决 。 这是列表中第一个需要递归才能解决的问题 。

推荐阅读