dongyukui8330 2012-07-02 21:56
浏览 17
已采纳

PHP逻辑 - 如何处理分数关系?

I'm trying to distribute prizes based on scores. I'm having trouble with my logic when it comes to TIE's. Can anyone give me logic pointers on handling a tie between 3 or more people?

UPDATE: The Goal is this --

  1. Make an ARRAY of the people who have tied (Only them)
  2. Know what POSITION those people are in.

I have a few sample phases you can skim through:

Example 1 - Works with 0 Ties

<?php
function give_prize($a, $b) {return;}

$prize = array(500, 250, 75);

$user = array(
    'user1' => 650,
    'user2' => 500,
    'user3' => 200,
    'user4' => 100,
);

$prize_count = count($prize);

for ($i = 0; $i < $prize_count; $i++) {
    give_prize($user[$i], $prize[$i]);
}

Example 2 - Works with 1 Tie (Is it a good way?)

<?php   
for ($i = 0; $i < $prize_count; $i++) {

    if (isset($user[$i+1])) {
        if ($user[$i] == $user[$i++]) {
            // My Tie breaker code
        }
    }
}

But what would I do in a tie of 3 or 4 people? Should I follow the above and do more if checks?

  • 写回答

1条回答 默认 最新

  • duanquan1207 2012-07-02 22:05
    关注

    I would start by grouping users by score and then sorting those groups in descending order. Once that is complete it will be easier to assign prizes no matter what rules you have.

    $usersByScore = array();
    foreach ($user as $name => $score) {
        $usersByScore[$score][] = $name;
    }
    krsort($usersByScore);
    

    For example, this turns the input $user

    $user = array(
        'user1' => 500,
        'user2' => 400,
        'user3' => 750,
        'user4' => 500,
    );
    

    into $usersByScore:

    $usersByScore = array(
        750 => array('user3'),
        500 => array('user1', 'user4'),
        400 => array('user2'),
    );
    

    Now you can issue prizes however you like.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大