php在一组数字中的重要性

前端之家收集整理的这篇文章主要介绍了php在一组数字中的重要性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一组数字,例如
$input = array(1,4,7,9,8,6,2,5,3);

我试图根据以下规则计算每个数字的重要性:

As the sequence gets longer the numbers get less significant,and each time a number is mentioned then it will improve the relevance (how much depends on its position in the
sequence).

我期待的是:

Array(
   '4' => 90%
   '1' => 75%
   '7' => 60%
   ....
)

所以4是最重要的,然后是1然后是7等.注意输出是完全捏造的,但是表明4应该是最重要的.我相信我想要某种线性解决方案.

@H_403_16@
@H_403_16@
这更像是你在想什么?答案基于仍然
$numbers = array(1,3);
$weight = array();
$count = count($numbers);

for ($i=0; $i<$count; $i++) {
  if (!isset($weight[$numbers[$i]])) $weight[$numbers[$i]] = 1;
  $weight[$numbers[$i]] += $count + pow($count - $i,2);
}

$max = array_sum($weight);
foreach ($weight as &$w) {
  $w = ($w / $max) * 100;
}

arsort($weight);

结果:

Array
(
    [4] => 34.5997286296
    [7] => 17.3677069199
    [1] => 16.3500678426
    [8] => 10.0407055631
    [9] => 9.29443690638
    [6] => 5.42740841248
    [2] => 4.40976933514
    [5] => 1.35685210312
    [3] => 1.15332428765
)
@H_403_16@

猜你在找的PHP相关文章