代码之家  ›  专栏  ›  技术社区  ›  SBB

PHP循环#到100,然后再循环另一个#

php
  •  0
  • SBB  · 技术社区  · 8 年前

    我正在研究一个快速函数,它将接受一个数字 $max 如果它在下面 100 ,计数依据 5's 高达 $最大值 .

    如果 $最大值 大于 100 ,它按5计,直到 100 ,然后按25秒计数,直到命中 $最大值 .

    例子:

    public function getDenominations($max)
    {
        $start = 20;
        $output = [];
    
        // If max is less than 100, count by 5's
        if($max <= 100 ){
            for($i = $start; $i <= $max; $i += 5) {
                array_push($output, $i);
            }
            return $output;
        // Max is greater than 100, count by 5's up to 100, then by 25's
        }else{
            for($i = $start; $i <= $max; $i += 25) {
                array_push($output, $i);
            }
            return $output;
        }
    
    }
    

    预期输出w/$最大值(&L);100:

    5,10,15,20,25,....100

    预计产量w/$最大值>100:

    5,10,15,20,25,....100,125,150,175,200

    我的问题是 $max > 100 ,我被卡住了,首先让它做正常的+5计数,然后是+25计数。目前,它只在25岁开始。

    电流输出时间 $max = 500 :

    Array ( [0] => 20 [1] => 45 [2] => 70 [3] => 95 [4] => 120 [5] => 145 [6] => 170 [7] => 195 [8] => 220 [9] => 245 [10] => 270 [11] => 295 [12] => 320 [13] => 345 [14] => 370 [15] => 395 [16] => 420 [17] => 445 [18] => 470 [19] => 495 )

    预计20-100(到5),然后100-500(到25)。

    4 回复  |  直到 8 年前
        1
  •  1
  •   OptimusCrime    8 年前

    我已经简化了你的代码。这不需要两个循环,只需保持循环,并用(5或25)更改当前值的增加量,直到达到最大值。

    例如如下所示:

    $max = 130;
    $output = [];
    $current = 0;
    
    // Keep looping until we hit max, or break out of the loop
    while ($current <= $max) {
        // Find out if we should increase the current value with 5 or 25
        if ($current < 100) {
            $current += 5;
        }
        else {
            $current += 25;
        }
    
        // Make sure we do not add anything above the current max (e.g. max = 120, current = 110 + 25 = 135)
        if ($current > $max) {
            break;
        }
    
        // Add the current value to the output array
        $output[] = $current;
    }
    
    print_r($output);
    

    输出

    Array 
    ( 
        [0] => 5 
        [1] => 10 
        [2] => 15 
        [3] => 20 
        [4] => 25 
        [5] => 30 
        [6] => 35 
        [7] => 40 
        [8] => 45 
        [9] => 50 
        [10] => 55 
        [11] => 60 
        [12] => 65 
        [13] => 70 
        [14] => 75 
        [15] => 80 
        [16] => 85 
        [17] => 90 
        [18] => 95 
        [19] => 100 
        [20] => 125 
    )
    

    Try it online here

        2
  •  0
  •   developer    8 年前

    像这样-在javascript中?

    max = 200
    for (i = 1; i < max; i++) {
        out = i * 5;
        console.log(out <= 100 ? out : (i * 5 * 5) - 400);
    }
    
    5
    10
    15
    20
    25
    30
    35
    40
    45
    50
    55
    60
    65
    70
    75
    80
    85
    90
    95
    100
    125
    150
    175
    200
    225
    250
    275
    300
    325
    350
    375
    400
    425
    450
    475
    500
    525
    550
    575
    600
    625
    650
    675
    700
    
        3
  •  0
  •   Hariprasad S H    8 年前
    function getDenominations($max)
    {
        $start = 20;
        $output = [];
        $incr = 5;
        $flag = false;
    
        for ($i = $start; $i <= $max; $i += $incr) {
            array_push($output, $i);
    
            if( $i >= 100 && $flag === false) {
                $incr = 25;
                $flag = true;
            }
    
        }
        return $output;
    }
    
    print_r(getDenominations(200));
    

    输出

    Array
    (
    [0] => 20
    [1] => 25
    [2] => 30
    [3] => 35
    [4] => 40
    [5] => 45
    [6] => 50
    [7] => 55
    [8] => 60
    [9] => 65
    [10] => 70
    [11] => 75
    [12] => 80
    [13] => 85
    [14] => 90
    [15] => 95
    [16] => 100
    [17] => 125
    [18] => 150
    [19] => 175
    [20] => 200
    )
    
        4
  •  0
  •   axiac    8 年前

    您需要的功能可以简单编写为:

    function getDenominations($max)
    {
        $start = 20;
        $output = [];
    
        // Count by 5 from $start to 100 or $max, whichever is smaller
        for ($i = $start; $i <= min($max, 100); $i += 5) {
            $output[] = $i;
        }
    
        // Continue counting by 25 from 125 to $max, if $max is large enough
        for ($i = 125; $i <= $max; $i += 25) {
            $output[] = $i;
        }
    
        return $output;
    }
    
    推荐文章