PHP头条
热点:

AA制分账-PHP源码


php代码

$v)
    	{
    		if($v > 0)
    		{
    			$arr_income[$k] = $v;
    		}elseif($v < 0)
    		{
    			$arr_pay[$k] = $v;
    		}
    	}
    	arsort($arr_income);
		asort($arr_pay);
		$this->income = $arr_income;
		$this->pay = $arr_pay;
	}
    function AA()
    {
    	foreach($this->income as $k=>&$v)
    	{
    		foreach($this->pay as $k1=>&$v1)
    		{
    			if($v + $v1 > 0)
    			{
    				$this->desc[] = $k.'给'.$k1.($v1 * -1).'元';
    				$this->income[$k] = $v + $v1;
    				unset($this->pay[$k1]);
    				$this->AA();
    			}else
    			{
    				$this->desc[] = $k.'给'.$k1.$v.'元';
    				$this->pay[$k1] = $v + $v1;
    				unset($this->income[$k]);
    				$this->AA();
    			}
    		}
    	}
    }
}
$accounts = array(
	'A'=>50, // A总共需要支付的金额为50
	'B'=>40,
	'C'=>10,
	'D'=>-20, // D需要收取的金券为20
	'E'=>-30,
	'F'=>-20,
	'G'=>-30,
	'H'=>0
);
$account = new account($accounts);
$account->AA();
print_r($account->desc);
// output :
// Array ( [0] => A给G30元 [1] => A给E20元 [2] => B给E10元 [3] => B给D20元 [4] => B给F10元 [5] => C给F10元)

www.phpzy.comtrue/php/34841.htmlTechArticleAA制分账-PHP源码 php代码 $v) { if($v > 0) { $arr_income[$k] = $v; }elseif($v income = $arr_income;$this->pay = $arr_pay;} function AA() { foreach($this->income as $k=>$v1) { if($v + $v1 > 0) { $this->desc[] = $k.给.$k1.($v1 * -1).元...

相关文章

PHP之友评论

今天推荐