2013-03-16 19 views
0

我有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; 

對於上面,並支付了六個地方的例子中,我們將計算作爲支出如下: 如果兩個人並列第一名,我們添加第一和第二名分數併除以二。 如果三個人並列第二名,我們增加第三,第四和第五個地點併除以三。 如果一個人單獨在第三名,這個人將獲得第六名的積分。

我可以指望的球員是誰在或並列某個地方的數量。

$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

我認爲一個數組在這裏會超級有用。 – Kermit 2013-03-16 16:50:16

+0

謝謝Aarolama。數組包含什麼?成績? – MikeH 2013-03-16 16:57:11

+0

分數和支出。 – Kermit 2013-03-16 16:57:38

回答

0

首先,有一個與你的獎金問題。如果把它們加起來你1.011
0.6 (1st) + 0.2 (2nd) + 0.15 (3rd) + 0.03 (4th) + 0.02 (5th) + 0.01 (6th) = 1.01

,這是比較容易,如果你讓你的PayoutsCounts成陣列 -
改變這些 -

$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

+0

謝謝你肖恩!這是令人印象深刻的代碼。我需要花一段時間研究一下我的頭腦。我喜歡你的想法,把它變成一個功能。實際上,根據有多少玩家出現,我們確實會改變支付和支付的地點。但我當然可以用這個工作。不夠感謝你! – MikeH 2013-03-16 23:25:38

+0

很高興我能幫助你。如果它有助於解決您的問題,請務必將其作爲答案。 – Sean 2013-03-16 23:34:15