简单程序题:abcd四个项目收益顺序分别占10%,20%,30%,40%

abcd四个项目收益顺序分别占10%,20%,30%,40%,当有一个项目不存在时则该项目收益归上一个项目,项目a 始终存在,封装一个函数得到abcd的收益;

这题不难,关键点是要想到用数组,这点想到了,那肯定能做出来。

程序代码:

<?php
/**
     * 获取项目收益
     * @param null $b
     * @param null $c
     * @param null $d
     * @return array
*/
    function getIncome($b = null, $c = null, $d = null)
    {
        $income = ['a' => 0.1, 'b' => 0.2, 'c' => 0.3, 'd' => 0.4];
        if (!$d) {
            $income['c'] += $income['d'];
            $income['d'] = 0;
        }
        if (!$c) {
            $income['b'] += $income['c'];
            $income['c'] = 0;
        }
        if (!$b) {
            $income['a'] += $income['b'];
            $income['b'] = 0;
        }
        return $income;
    }
 echo '<pre>';
    var_dump(getIncome(1, 0, 0));
?>

posted @ 2016-11-30 23:49  暗号已存在  阅读(669)  评论(0编辑  收藏  举报