加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php在一组数字中的重要性

发布时间:2020-12-13 18:00:47 所属栏目: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 dep
我有一组数字,例如
$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应该是最重要的.我相信我想要某种线性解决方案.

这更像是你在想什么?答案基于仍然
$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
)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读