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

请教大家一个问题,  具体是问题是这样的,
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 "
";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);	}}?>



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'));
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)

Statement
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
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment