代码之家  ›  专栏  ›  技术社区  ›  arma Mandip Darji

检查整数是否在任何范围内的更快方法?

php
  •  0
  • arma Mandip Darji  · 技术社区  · 14 年前

    我想知道有没有其他方法可以缩短这个?下面是我想要做同样事情的例子,只是简短一些。

    if($c <= 100){
        echo 'A';
    }elseif($c <= 200 && $c > 100){
        echo 'B';
    }elseif($c <= 300 && $c > 200){
        echo 'C';
    }elseif($c <= 400 && $c > 300){
        echo 'D';
    }elseif($c <= 500 && $c > 400){
        echo 'E';
    }elseif($c <= 600 && $c > 500){
        echo 'F';
    }elseif($c <= 700 && $c > 600){
        echo 'Z';
    }elseif($c <= 800 && $c > 700){
        echo 'H';
    }elseif($c <= 900 && $c > 800){
        echo 'I';
    }elseif($c < 1000 && $c > 900){
        echo 'K';
    }elseif($c <= 1100 && $c > 1000){
        echo 'L';
    }elseif($c <= 1200 && $c > 1100){
        echo 'M';
    }elseif($c < 1300 && $c > 1200){
        echo 'N';
    }elseif($c <= 1400 && $c > 1300){
        echo 'O';
    }elseif($c <= 1500 && $c > 1400){
        echo 'P';
    }elseif($c <= 1600 && $c > 1500){
        echo 'Q';
    }elseif($c <= 1700 && $c > 1600){
        echo 'R';
    }elseif($c <= 1800 && $c > 1700){
        echo 'S';
    }elseif($c <= 1900 && $c > 1800){
        echo 'T';
    }elseif($c <= 2000 && $c > 1900){
        echo 'V';
    }elseif($c <= 2100 && $c > 2000){
        echo 'X';
    }else{
        echo 'AA';
    }
    
    5 回复  |  直到 14 年前
        1
  •  2
  •   user187291    14 年前

    更快、更短——没有*,但你可以让它更灵活、更优雅。

    function find_range($n, $ranges) {
        foreach($ranges as $key => $range)
            if($n >= $range[0] && $n <= $range[1])
                return $key;
        return false;
    }
    
    $ages = array(
        'baby'   => array(0, 1),
        'child'  => array(2, 13),
        'teen'   => array(14, 19),
        'adult'  => array(20, 59),
        'senior' => array(60, 100)
    );
    
    var_dump(find_range(20, $ages));
    

    (*假设范围是任意的。如果我们更多地了解范围,例如,它们被排序,或者总是相交,或者遵循某种公式,我们可以找到更好的算法)。

        2
  •  0
  •   Jamie Rumbelow    14 年前

    我想你可以写一个辅助函数,它可以得到一个充满匿名函数的数组。如果要执行的代码是好的和简单的,它会很好地工作。否则,如果你在php5.3上,你可以使用适当的lambda来做更复杂的事情。那会使它更整洁。

    function integer_in_ranges($integer, $array) {
        foreach ($array as $range => $function) {
            if ($integer < $range) {
                $results[] = $function();
            }
        }
    
        return $results;
    }
    
    integer_in_ranges($c, array(
        101 => create_function('', "execute_function(); return TRUE;"),
        202 => create_function('', "execute_other_function(); return TRUE;"),
        // ...
    ));
    
        3
  •  0
  •   KingCrunch    14 年前

    不是内置的,但因为你的计划没有那么复杂

    switch ((int) (($c - 1) / 100)) {
        case 0: /* action here */ break;
        case 1: /* action here */ break;
        case 2: /* action here */ break;
        case 3: /* action here */ break;
        default: /* action here */ break;
    }
    

    另一种适用于任意限制的解决方案。我想有一个上限

    $c; // value to test
    $ranges = array(101,201,199); // and so on
    $index = 0;
    while ($ranges[$index] >= $c) { $index++; }
    // Do something useful with "$index"
    
        4
  •  0
  •   symcbean    14 年前

    这真的是你的代码吗?如果C是100的倍数,你总是想返回AA?

    用最少的代码获得相同结果的最快方法是……

    $res=array('A','B','C',....);
    if (($c % 100) && ($c>0) && ($c<2100)) {
       echo $res[(integer)($c/100)];
    } else {
       echo 'AA';
    }
    

    但我会用一种类似于青蛙甾醇的溶液。

        5
  •  0
  •   StuartLC    14 年前

    如果分支的数目很大,您可能需要查看 binary search tree 这将减少任何特定分支需要测试的最大比较数(尽管最小测试数也将增加)。

    例如,以下情况下,至少有两个测试,最多有三个测试。在上面的示例中,最多有4个测试。

    if ($c< 202)
    {
      if($c < 101){ 
        // action 101 here 
      }
      else { 
        // action 202 here 
      }
    }
    else {
      if($c < 303){ 
        // action here 
      }
      elseif($c < 404){ 
       // action here 
    }
    

    编辑:取决于你所说的“缩短”是什么意思。根据你的标题“更快”我假设性能。

    编辑(您已经更新了列表):“A”点中的例子只需要一个比较测试,而“AA”的失败将导致25个测试失败。假设数据在所有的容器中均匀分布,测试的平均数量将为~12(‘L’),而在二叉树形成中,大多数检查将是log2(n)+1-即6个测试将覆盖32个分支。不幸的是,在您的例子中,偶数'100'上的数据中存在空白。如果排除这些空白(即,如果值mod 100==0,则降至'a a'),则可以对其余排列进行单侧测试。