我有一个40人的高尔夫联盟。我们都会在底池中投入资金并根据最终得分支付前6个位置。
如果没有关系,那么支付就会很简单,但通常我们会有2个人并列第一名,3人并列第二名,1人独占第三名等等。这些变化似乎无穷无尽。
我一直在尝试使用PHP自动计算每个地方的计算付款但是没有成功。任何建议,帮助或指向正确的方向将非常感激。我注意到其他人试图在这个网站上提出类似的问题,但没有成功地提出问题。我会尽力做得更好。
以下是我一直在玩的一些数据:
$playerNumber=40;
$totalPoints=100;
付款:
$pointsFirst=0.6*$totalPoints;
$pointsSecond=0.2*$totalPoints;
$pointsThird=0.15*$totalPoints;
$pointsFourth=0.03*$totalPoints;
$pointsFifth=0.02*$totalPoints;
$pointsSixth=0.01*$totalPoints;
对于上面给出的示例并支付6个名额,我们将按以下方式计算支出: 如果两个人并列第一名,我们加上第一和第二位并除以2。 如果三个人并列第二名,我们添加第三,第四和第五位并除以三。 如果一个人独自排在第三位,那么这个人将赢得第六名。
我可以计算在某个地方进入或并列的球员数量。
$countFirst=2;
$countSecond=3;
$countThird=1;
$countFourth=2;
$countFifth=1;
$countSixth=2;
在此示例中,玩家得分为72,72,73,73,73,74,75,75,76,77,77。
起初我以为这是嵌套数组的应用程序。然后我想也许使用数组,数组切片等可能是一种方法。每次我最终都在树林里。我没有看到逻辑。
我已经使用条件语句支付了三个地方,但是用这种方法支付六个名额让我深入了解。
使用条件语句向三个地方付款的示例:
$pointsFirst=0.5*$totalPoints;
$pointsSecond=0.3*$totalPoints;
$pointsThird=0.2*$totalPoints;
if($countFirst>2) {
$ptsA=round($totalPoints/$countFirst,2);
}
elseif($countFirst==2) {
$ptsA=round(($pointsFirst+$pointsSecond)/2,2);
if($countSecond>1) {
$ptsB=round($pointsThird/$countSecond,2);
}
elseif($countSecond==1) {
$ptsB=round($pointsThird,2);
}
}
elseif($countFirst==1) {
$ptsA=round($pointsFirst,2);
if($countSecond>1) {
$ptsB=round(($pointsSecond+$pointsThird)/2,2);
}
elseif($countSecond==1) {
$ptsB=round($pointsSecond,2);
if($countThird>1) {
$ptsC=round($pointsThird/$countThird,2);
}
elseif($countThird==1) {
$ptsC=round($pointsThird,2);
}
}
}
我希望我的要求清楚。我很乐意澄清任何事情。如果有人对如何有效地将支付计算自动化到六个地方有任何想法,我将永远感激。谢谢!麦克
按要求:
$scores=array();
$scores[0]=72;
$scores[1]=72;
$scores[2]=73;
$scores[3]=73;
$scores[4]=73;
$scores[5]=74;
$scores[6]=75;
$scores[7]=75;
$scores[8]=76;
$scores[9]=77;
$scores[10]=77;
$payout=array();
$payout[0]=0.6*$totalPoints;
$payout[1]=0.2*$totalPoints;
$payout[2]=0.15*$totalPoints;
$payout[3]=0.03*$totalPoints;
$payout[4]=0.02*$totalPoints;
$payout[5]=0.01*$totalPoints;
$countScores=array();
$countScores[0]=$countFirst;
$countScores[1]=$countSecond;
$countScores[2]=$countThird;
$countScores[3]=$countFourth;
$countScores[4]=$countFifth;
$countScores[5]=$countSixth;
答案 0 :(得分:0)
首先,您的付款存在问题。如果您添加它们,则会1.01
而不是1
0.6 (1st) + 0.2 (2nd ) + 0.15 (3rd) + 0.03 (4th) + 0.02 (5th) + 0.01 (6th) = 1.01
第二次,如果您将Payouts
和Counts
放入数组中会更容易 -
改变这些 -
$pointsFirst=0.6*$totalPoints;
$pointsSecond=0.2*$totalPoints;
$pointsThird=0.15*$totalPoints;
$pointsFourth=0.03*$totalPoints;
$pointsFifth=0.02*$totalPoints;
$pointsSixth=0.01*$totalPoints;
$countFirst=2;
$countSecond=3;
$countThird=1;
$countFourth=2;
$countFifth=1;
$countSixth=2;
到这些
$payout=array();
$payout[0]=0.6*$totalPoints;
$payout[1]=0.2*$totalPoints;
$payout[2]=0.15*$totalPoints;
$payout[3]=0.03*$totalPoints;
$payout[4]=0.02*$totalPoints;
$payout[5]=0.01*$totalPoints;
$count=array();
$count[0]=2;
$count[1]=3;
$count[2]=1;
$count[3]=2;
$count[4]=1;
$count[5]=2;
这是一种方法的开始。虽然我最终会将其更改为一个函数,以便我可以使用不同的支出和地点数量再次使用它(请参阅下面的phpfiddle示例) 我在4个步骤中看到了这一点 -
第1步
// Add together the payments if there are ties - ie. 2 tied for first $payout[0]+$payout[1], etc
$payout_groups = array(); // start a payout array
$payout_groups_key = 0; // array key count
$payout_groups_count = 0; // array counter, use to match the $count array values
for($w=0;$w<count($payout);$w++){ //
if(array_key_exists($payout_groups_key,$payout_groups)){
$payout_groups[$payout_groups_key] += $payout[$w]; // if there are ties, add them together
}
else{
$payout_groups[$payout_groups_key] = $payout[$w]; // else set a new payout level
}
$payout_groups_count++; // increase the counter
if($payout_groups_count == $count[$payout_groups_key]){ // if we merged all the ties, set a new array key and restart the counter
$payout_groups_key++;
$payout_groups_count = 0;
}
}
第2步
// basic counter to get how many placers/winners. This makes it possible to have ties for 6th (last) place
$x = 0;
$y = 0;
while($y < count($payout)){
$y += $count[$x]; // the $count array values until we reach the amount of places/payouts
$x++;
}
第3步
// Create array for winnings per placing
$winnings = array(); // start an array
$placings_count = 0; //
$placings_counter = 0;
for($z=0;$z<$y;$z++){
$winnings[$z] = $payout_groups[$placings_count]/$count[$placings_count];
$placings_counter++;
if($placings_counter == $count[$placings_count]){
$placings_count++;
$placings_counter = 0;
}
}
第4步
// Assign winnings to scorecard
$scoreboard = array();
for($t=0;$t<count($winnings);$t++){
$scoreboard[$t]['score'] = $scores[$t];
$scoreboard[$t]['payout'] = $winnings[$t];
}
您可以使用 - http://phpfiddle.org/main/code/a1g-qu0
中定义的值来查看此内容使用上面相同的代码,我更改了付款金额,并将其增加到第7位 - http://phpfiddle.org/main/code/uxi-qgt