gpt4 book ai didi

php - 需要逻辑帮助,制作三维数组(PHP)

转载 作者:行者123 更新时间:2023-12-04 06:30:25 24 4
gpt4 key购买 nike

我需要帮助,例如,我在那里得到了 4 个团队名称的数组。

array('logiX.L4d22','Lust','Marat and Friends','比利时 Pandas ');

我想制作 3 维数组,其中第一个是一轮,第二个是比赛,第三个是互相比赛的球队,总会有 2 支球队。

逻辑必须是所有球队都必须与所有其他球队一起比赛,并且在一轮中,任何球队只会打一场比赛,所以如果我们有 5 支球队,那么在某一轮中,一支球队必须等待下一轮。

它必须产生这样的东西:

大批
0 =>
大批
0 =>
大批
0 => 字符串“logiX.L4D2”(长度=10)
1 => 字符串 'Lust'(长度 = 4)
1 =>
大批
0 => 字符串 'Marat and Friends'(长度 = 17)
1 => 字符串“比利时 Pandas ”(长度=17)
1 =>
大批
0 =>
大批
0 => 字符串“logiX.L4D2”(长度=10)
1 => 字符串 'Marat and Friends'(长度 = 17)
1 =>
大批
0 => 字符串 'Lust'(长度 = 4)
1 => 字符串“比利时 Pandas ”(长度=17)
2 =>
大批
0 =>
大批
0 => 字符串“logiX.L4D2”(长度=10)
1 => 字符串“比利时 Pandas ”(长度=17)
1 =>
大批
0 => 字符串 'Lust'(长度 = 4)
1 => 字符串 'Marat and Friends'(长度 = 17)

它必须与 2,3,5 ...10 ...12 个团队一起工作。

我希望你能帮助我,我已经花了 1 天半的时间。

最佳答案

一些谷歌搜索 循环算法 PHP 给出以下内容:

http://speedtech.it/blog/2009/03/15/round-robin-algorithm-php/

http://www.phpbuilder.com/board/showthread.php?t=10300945

我希望你能找到你要找的东西。

编辑

添加我对此的尝试,遵循 round-robin algorithm described on Wikipedia .

如果团队编号为奇数,则在数组中添加一个团队(空值),以便您可以检索每一轮的“等待团队”。

<?php

$teams = range('a', 'g');

function make_rounds($teams)
{
$nb_teams = count($teams);

if ($nb_teams % 2 != 0)
{
$teams[] = null;
$nb_teams++;
}

$nb_rounds = $nb_teams - 1;
$nb_matches = $nb_teams / 2;

$rounds = array();

for($round_index = 0; $round_index < $nb_rounds; $round_index++)
{
$matches = array();

for($match_index = 0; $match_index < $nb_matches; $match_index++)
{
if ($match_index == 0)
$first_team = $teams[0];
else
$first_team = $teams[(($nb_teams-2) + $match_index - $round_index) % ($nb_teams-1) + 1];

$second_team = $teams[(($nb_teams*2) - $match_index - $round_index - 3) % ($nb_teams-1) + 1];

$matches[] = array($first_team, $second_team);
}

$rounds[] = $matches;
}

return $rounds;
}

print_r(make_rounds($teams));

关于php - 需要逻辑帮助,制作三维数组(PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5485728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com