Home > Backend Development > PHP Tutorial > 有1000块钱,3个人按比例分,如何设计比较合理

有1000块钱,3个人按比例分,如何设计比较合理

WBOY
Release: 2016-06-23 13:29:21
Original
7128 people have browsed it

请教大家一个问题,  具体是问题是这样的,
1、有1000块钱,A B C 3个人分最多可获得的比例 是30% 40% 30%
2、如果A和B两个人分 A 30% B 70%
3、如果B和C两个人分  B 40%  C 70%
4、若果只有一个人分  A 最多可以分30%
                                   B最多可以分40%
                                   C最多可以分70%


回复讨论(解决方案)

希望可以用面向对象的思想来实现

这是什么怪帐?
请说清楚点

我的理解....你说最多可分得,说明钱是可以剩余的(若不可剩余,30% 40% 30%这个分配就不对)

$member=array('a','b','c');//$member=array('a','b');//$member=array('b','c');//$member=array('b');$res = allotment(1000,$member);echo "<pre class="brush:php;toolbar:false">";print_r($res);echo "
Copy after login
";function allotment($money,$member){ $ms = array('a','b','c'); $abc = array( 'a' => 30, 'b' => 40, 'c' => 30, ); $ab = array( 'a' => 30, 'b' => 70, ); $bc = array(//B 40% C 70% 一共就 110%了,所以c 改为 60% 'b' => 40, 'c' => 60, ); $one = array( 'a' => 30, 'b' => 40, 'c' => 70, ); $same = array_intersect($ms,$member); sort($same); $res=array(); if(count($same)==1){ $data[$same[0]] = $one[$same[0]]; }else{ $plan = join('',$same); $data = $$plan; } $used = 0; foreach($data as $k=>$v){ $rate = rand(1,$v); $res[$k]['rate']=$rate.'%';//百分比 $res[$k]['mon']=$money * ($rate / 100); $used += $res[$k]['mon']; } $res['r'] = $money - $used;//剩余钱 return $res;}

3、如果B和C两个人分 B 40% C 70% 这个应该是 B 40% C 60%吧

<?php$result = assign(1000, 1, 2, 3);print_r($result);$result = assign(1000, 1, 2, 0);print_r($result);$result = assign(1000, 0, 2, 3);print_r($result);$result = assign(1000, 1, 0, 0);print_r($result);$result = assign(1000, 0, 2, 0);print_r($result);$result = assign(1000, 0, 0, 3);print_r($result);function assign($amount, $a=0, $b=0, $c=0){	if($amount==0 || $a==0 && $b==0 && $c==0){		return false;	}	if($a>0 && $b>0 && $c>0){		return array('a'=>$amount*0.3, 'b'=>$amount*0.4, 'c'=>$amount*0.3);	}	if($a>0 && $b>0 ){		return array('a'=>$amount*0.3, 'b'=>$amount*0.7, 'c'=>0);	}	if($b>0 && $c>0){		return array('a'=>0, 'b'=>$amount*0.4, 'c'=>$amount*0.6);	}	if($a>0){		return array('a'=>$amount*0.3, 'b'=>0, 'c'=>0);	}	if($b>0){		return array('a'=>0, 'b'=>$amount*0.4, 'c'=>0);	}	if($c>0){		return array('a'=>0, 'b'=>0, 'c'=>$amount*0.7);	}}?>
Copy after login



Array
(
[a] => 300
[b] => 400
[c] => 300
)
Array
(
[a] => 300
[b] => 700
[c] => 0
)
Array
(
[a] => 0
[b] => 400
[c] => 600
)
Array
(
[a] => 300
[b] => 0
[c] => 0
)
Array
(
[a] => 0
[b] => 400
[c] => 0
)
Array
(
[a] => 0
[b] => 0
[c] => 700
)

这是什么怪帐?
请说清楚点


不好意思啊,可能是我描叙的不太清楚,是这样的:
有1000块钱,A B C 3个人分最多可获得的比例 是A30% B70% C100%
1、如果A,B,C3个人分,A分了30% 那么B只能分70%-30% C只能分100%-(70%-30%)
2、如果B,C两个人分, B可以分70% C分100%-70%
3,、依次类推 A,C 分,A,B分,,,,,,

干掉A和B,C拿走跑路

这个意思?

function foo($u, $m=1000) {  if(! is_array($u)) $u = array($u);  echo join(',', $u), PHP_EOL;  $d = array('A' => 0.3, 'B' => 0.4, 'C' => 0.3);  foreach($u as $k) {    $r[$k] = $d[$k] * $m;    unset($d[$k]);  }  if(count($u) == 1 || count($d) == 0) return $r;  foreach($d as $v) $r[$k] += $v * $m;   return $r;}print_r(foo(array('A', 'B', 'C')));print_r(foo(array('A', 'B')));print_r(foo(array('A', 'C')));print_r(foo(array('B', 'C')));print_r(foo(array('B')));print_r(foo('C'));
Copy after login
A,B,CArray(    [A] => 300    [B] => 400    [C] => 300)A,BArray(    [A] => 300    [B] => 700)A,CArray(    [A] => 300    [C] => 700)B,CArray(    [B] => 400    [C] => 600)BArray(    [B] => 400)CArray(    [C] => 300)
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template