-2
A
回答
0
這絕對不是要做到這一點的最佳方式,請你自己優化它(這將僅偶數隊的工作):
<?php
$allTeams = [0,1,2,3,4,5];
$everyGames = [];//Append team matches to this
foreach($allTeams as $team1){
foreach($allTeams as $team2){
if (!in_array([$team2, $team1], $everyGames)
&& $team2 !== $team1) {
$everyGames[] = [$team1, $team2];
}
}
}
$copyOfEveryGames = $everyGames;
$needToLoopAgain = true;
while($needToLoopAgain){
$allRounds = [];
$needToLoopAgain = false;
$everyGames = $copyOfEveryGames;
shuffle($everyGames);//Shuffle it for the random array
while(count($everyGames) > 0){
$allMatches = [];
$tempEveryGames = $everyGames;
for($i = 0; $i < count($allTeams)/2; $i++){
foreach($tempEveryGames as $key => $game){
if(!(in_array_r($game[0], $allMatches) || in_array_r($game[1], $allMatches))){
$allMatches[] = $game;
unset($tempEveryGames[$key]);
break;
}
}
}
if(count($allMatches) === count($allTeams)/2){
$everyGames = $tempEveryGames ;
$allRounds[] = $allMatches;
}else{
$needToLoopAgain = true;
break;
}
}
if(count($allRounds) !== count($allTeams) -1){
$needToLoopAgain = true;
}
}
//Display Example
foreach($allRounds as $round){
print_r($round);
echo "<br>";
}
/*
Example Output:
Array ([0] => Array ([0] => 0 [1] => 3)
[1] => Array ([0] => 1 [1] => 4)
[2] => Array ([0] => 2 [1] => 5))
Array ([0] => Array ([0] => 0 [1] => 2)
[1] => Array ([0] => 4 [1] => 5)
[2] => Array ([0] => 1 [1] => 3))
Array ([0] => Array ([0] => 1 [1] => 2)
[1] => Array ([0] => 3 [1] => 4)
[2] => Array ([0] => 0 [1] => 5))
Array ([0] => Array ([0] => 1 [1] => 5)
[1] => Array ([0] => 0 [1] => 4)
[2] => Array ([0] => 2 [1] => 3))
Array ([0] => Array ([0] => 0 [1] => 1)
[1] => Array ([0] => 2 [1] => 4)
[2] => Array ([0] => 3 [1] => 5))
*/
//Copied from http://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
?>
0
我希望這會回答你的問題,如果我很好地理解你的問題。
function againstWho(array $teams) : array{
$fixture = null;
foreach($teams as $key => $team){
unset($teams[$key]);
$fixture[$team] = $teams;
$teams[] = $team;
}
return $fixture;
}
// EXAMPLE
$teams = ['MANU', 'ARS', 'LIV', 'CHE', 'MANC'];
foreach (againstWho($teams) as $team => $fixtures){
echo "$team will face ";
foreach ($fixtures as $key => $rival){
echo "$rival, ";
}
echo "<br />";
}
// Her's what gets printed
/*
MANU will face ARS, LIV, CHE, MANC,
ARS will face LIV, CHE, MANC, MANU,
LIV will face CHE, MANC, MANU, ARS,
CHE will face MANC, MANU, ARS, LIV,
MANC will face MANU, ARS, LIV, CHE
*/
相關問題
- 1. 隨機洗牌數組
- 2. 隨機隨機洗牌C++數組(每次不同)
- 3. 隨數組中包含的最高數字隨機洗牌
- 4. 隨機洗牌在PHP?
- 5. 洗牌.txt文件隨機
- 6. 列的隨機洗牌
- 7. 無法隨機洗牌
- 8. 洗牌的隨機化
- 9. N-Puzzle僞隨機洗牌?
- 10. 隨機洗牌列表
- 11. C++向量隨機隨機洗牌的一部分
- 12. 隨機洗牌和函數指針
- 13. 需要使用密鑰的可重複的隨機數組隨機洗牌
- 14. 創建一個部分洗牌的隨機數排序列表
- 15. 在Python中隨機化/洗牌列表/數組?
- 16. 隨機分區與分區然後洗牌
- 17. 洗牌列表中隨機的Java
- 18. 隨機洗牌錯誤信息
- 19. 與jQuery堆棧隨機化/洗牌卡
- 20. 隨機洗牌<table>列
- 21. 從資源隨機洗牌文本
- 22. 隨機洗牌的值的網格?
- 23. 隨機洗牌列除第一列
- 24. 如何隨機隨機洗牌地圖中的值?
- 25. 隨機隨機洗牌列表變爲無
- 26. 在Python中隨機隨機洗牌的鍵和值DIctionary
- 27. 如何在Python中進行隨機但部分洗牌?
- 28. 測試卡牌洗牌機
- 29. 洗牌在Objective-C數組
- 30. 如何洗牌數組值
請說明你已經做了什麼來嘗試解決這個問題。 –