Home > Backend Development > PHP Tutorial > 求教一个快速查找算法

求教一个快速查找算法

WBOY
Release: 2016-06-06 20:38:10
Original
1006 people have browsed it

情景

有这样的对应关系:

<code>90~100 => a+
86 ~ 90 => a
80 ~ 85 =>a-
....
</code>
Copy after login
Copy after login

怎么样根据 (eg: score = 81) 最高效的找到对应的等级

回复内容:

情景

有这样的对应关系:

<code>90~100 => a+
86 ~ 90 => a
80 ~ 85 =>a-
....
</code>
Copy after login
Copy after login

怎么样根据 (eg: score = 81) 最高效的找到对应的等级

<code>function getRank($score) {
    $rank = array(90=> "A+", 86=>"A", 80=>"A-", 75=>"B+", 70=>"B", 65=>"B-");
    foreach($rank as $s => $r) {
        if($score - $s >= 0) return $r;
    }
    return false;
}
echo getRank(81);
</code>
Copy after login

感觉这样写应该就可以了。不过看你的题目情况应该是判试卷分数吧?如果极度追求效率且分数没有小数出现的话,可以采取”将0-100所有分数的等级给出一个数组,则$rank[$score]就直接是对应等级了“这种空间换时间的方法。

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template