Algorithm to calculate how many times through the intersection

<?php
/**
 * 假设某人有100000现金,经过一个路口交一次费用。
 * 规则为,大于50000交5%。小于等于50000,交5000。
 * 请计算此人可以经过多少次路口。
 * 直接看答案就没意思了,自己思考后才有意思
 */
function getRoadCount() {
    // 初始化数据
    $money = 100000;
    $count = 0;

    // 第一关
    while($money > 50000) {
        $count++;
        $money *= 0.95;
    }

    // 第二关
    while($money >= 5000) {
        $count++;
        $money -= 5000;
    }
    return $count;

}

echo getRoadCount(); // 23次

Guess you like

Origin www.cnblogs.com/jiqing9006/p/12185370.html