-1

我试图形成一个循环来返回锦标赛树中的下一个比赛号码。我设法做了上括号,但下括号我遇到了麻烦。

你可以看看这张照片,确切地知道我在找什么: http ://s21.postimg.org/3ryr344br/bracket.png (这是一个例子,如果 max = 32)

因此,例如在显示 32 的图像中,下一个匹配编号应返回 40。匹配编号 44 或 45 应返回 50。

任何帮助表示赞赏。非常感谢

如果最大值 = 8

8 = 10 = 第一轮开始

9 = 11

10 = 12 = 第二轮开始

11 = 12

12 = 13 = 进入最后一轮

如果最大值 = 16

16 = 20 = 第一轮开始

17 = 21

18 = 22

19 = 23

20 = 24 = 第二轮开始

21 = 24

22 = 25

23 = 25

24 = 26 = 第三轮开始

25 = 27

26 = 28 = 第四轮开始

27 = 28

28 = 29 = 进入最后一轮

如果最大值 = 32

32 = 40 = 第一轮开始

33 = 41

34 = 42

35 = 43

36 = 44

37 = 45

38 = 46

39 = 47

40 = 48 = 第二轮开始

41 = 48

42 = 49

43 = 49

44 = 50

45 = 50

46 = 51

47 = 51

48 = 52 = 第三轮开始

49 = 53

50 = 54

51 = 55

52 = 56 = 第四轮开始

53 = 56

54 = 57

55 = 57

56 = 58 = 第五轮开始

57 = 59

58 = 60 = 第六轮开始

59 = 60

60 = 61 = 进入最后一轮

最大值可达 512

4

1 回答 1

0

我前段时间做过类似的事情,但是使用 sql 表来保存数据。也许这段代码会对你有所帮助:

    $participants = 32;
    $games = participantsToGames($participants);
    $rounds = log($participants, 2);

    echo "#Participants: $participants; games: $games; rounds: $rounds <br />";

    echo "TRUNCATE TABLE `participants`;<br />";
    for($i = 1; $i <= $participants; $i++){
        echo "INSERT INTO `participants` (`title`) VALUES ('Driver $i');<br />";
    }

    echo "TRUNCATE TABLE `games`;<br />";
    $participantsLeft = $participants;
    $gameNumber = 1;
    $gamesAfterLastRound = 1;

    for($round = 1; $round <= $rounds; $round++){
        $nrOfGames = $participantsLeft / 2;

        for($gameNrThisRound = 1; $gameNrThisRound <= $nrOfGames; $gameNrThisRound++, $gameNumber++){
            $nextGame = ($participantsLeft == 2) 
                ? 0 
                : $gamesAfterLastRound -1 + ceil($nrOfGames + $gameNrThisRound / 2);

            $nextGamePosition = ($gamesAfterLastRound -1 + ($nrOfGames + $gameNrThisRound / 2) == $nextGame) 
            ? 'b'
            : 'a'; 

            if ($round == 1){
                $a = ($participantsLeft * $gameNrThisRound / $nrOfGames) - 1;
                $b = $a + 1;
                echo $sql = "INSERT INTO `games` (`participant_a`, `participant_b`, `round`, `game_number`, `next_game`, `next_game_position`) "
                        ."VALUES ('$a', '$b', '$round', '$gameNumber', '$nextGame', '$nextGamePosition');<br />";
            }elseif ($round == $rounds){
                echo $sql = "INSERT INTO `games` (`round`, `game_number`, `next_game`, `next_game_position`) "
                        ."VALUES ('$round', '$gameNumber', '0', '0');<br />";
                $gameNumber++;
                echo $sql = "INSERT INTO `games` (`round`, `game_number`, `next_game`, `next_game_position`) "
                        ."VALUES ('$round', '$gameNumber', '0', '0');<br />";
            }else{
                echo $sql = "INSERT INTO `games` (`round`, `game_number`, `next_game`, `next_game_position`) "
                        ."VALUES ('$round', '$gameNumber', '$nextGame', '$nextGamePosition');<br />";
            }


        }
        echo '#<br />';
        $gamesAfterLastRound = $gameNumber;
        $participantsLeft /= 2;

    }
    echo "UPDATE `games` SET `next_game` = 32 WHERE `next_game`=31";


    function participantsToGames($participants){
        if ($participants == 2) return 1;
        return participantsToGames($participants / 2) + ($participants / 2);
    }






    /*
     * 
     CREATE TABLE IF NOT EXISTS `games` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `participant_a` int(10) unsigned NOT NULL,
      `participant_b` int(10) unsigned NOT NULL,
      `winner` int(10) unsigned NOT NULL,
      `round` smallint(5) unsigned NOT NULL,
      `game_number` int(10) unsigned NOT NULL,
      `next_game` int(10) unsigned NOT NULL,
      `next_game_position` enum('a','b') NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;


    CREATE TABLE IF NOT EXISTS `participants` (
      `id` mediumint(9) NOT NULL AUTO_INCREMENT,
      `title` varchar(256) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;
*/

只需将这两个表添加到数据库中,然后执行此脚本返回的 SQL 查询。您将有一张桌子供参与者使用,另一张桌子供游戏插槽使用。

于 2013-09-29T16:42:08.827 回答