循环返回下一个匹配号码

时间:2013-09-29 16:22:53

标签: php loops if-statement for-loop brackets

我正在尝试在锦标赛树中返回下一个匹配号码。 我设法做了上支架但下支架我遇到了麻烦。

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

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

感谢任何帮助。 非常感谢

如果max = 8

8 = 10 =第一轮开始

9 = 11

10 = 12 =第二轮开始

11 = 12

12 = 13 =到最后一轮

如果max = 16

16 = 20 =第一轮开始

17 = 21

18 = 22

19 = 23

20 = 24 =第二轮开始

21 = 24

22 = 25

23 = 25

24 = 26 =第3轮开始

25 = 27

26 = 28 =第4轮开始

27 = 28

28 = 29 =到最后一轮

如果max = 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 =第3轮开始

49 = 53

50 = 54

51 = 55

52 = 56 =第4轮开始

53 = 56

54 = 57

55 = 57

56 = 58 =第5轮开始

57 = 59

58 = 60 =第6轮开始

59 = 60

60 = 61 =到最后一轮

max最高可达512

1 个答案:

答案 0 :(得分: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 ;
*/

Jus将这两个表添加到数据库中,然后执行此脚本返回的SQL查询。您将有一个参与者表和其他游戏时段表。