【问题标题】:JavaScript - Creating an array for a knockout tournamentJavaScript - 为淘汰赛创建数组
【发布时间】:2013-03-13 17:46:42
【问题描述】:

我希望能够为淘汰赛生成空白比赛。这是我的意思的一个例子。

假设我们有 8 支球队参加的淘汰赛: 我使用Math.log(teamList.length)/Math.log(2) 计算出锦标赛有 3 轮。

以下是计算每轮比赛数的一般规则:

numberOfRounds = n    [ [2^n-1 matches], ...., [2^0 matches] ]

因此我知道,对于 8 支球队的锦标赛将有 3 轮,锦标赛将如下所示:

[ [4 matches], [2 matches], [1 match] ]

我应该指出,每场比赛都存储为一个数组,因此例如 8 队锦标赛的半决赛可能如下所示:

[ [team1,team2], [team3,team4] ]

我正在尝试生成一些代码,这意味着我可以获取球队列表,并为锦标赛生成一组空白比赛。

因此,如果我将 8 支球队的列表传递给锦标赛,则会生成以下匹配数组:

[
  [ [], [], [], [] ],
  [ [], [] ],
  [ [] ]
]

有人对如何做到这一点有任何想法吗?到目前为止,我只有以下内容:

for(var i = 0; i < numRounds; i++) {
      matches.push([]);
}

这会生成锦标赛的每一轮,因此对于 8 支球队,它会生成一个长度为 3 的数组,但我不知道如何在每轮内生成必要数量的比赛。

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:

    这应该为给定数量的团队生成一个空匹配数组:

    function genMatches (nTeams) {
        var matchArray = [];
        while (nTeams > 1) {
            nTeams = (nTeams + 1) >> 1;
            var matches = [];
            for (var i = 0; i < nTeams; ++i) {
                matches.push([]);
            }
            matchArray.push(matches);
        }
        return matchArray;
    }
    

    它应该正确处理不是 2 的幂的团队计数。它确实会生成一个轮空时段(当团队数量为奇数时)。

    【讨论】:

    • 太棒了,这个问题很容易在你的脑海中想到,但是把它放到一个函数中让我很困惑......感谢帮助。
    【解决方案2】:

    假设任何未与任何球队配对的球队已经有资格进入下一轮

    function genMatches(a, match = 0, stage = {}) {
      let isOdd = false
      if (a === 1) {
        return match;
      }
      if (a % 2) {
        isOdd = true;
        a = a - 1;
      }
      match = match + Math.floor(a / 2);
    
      stage[Object.keys(stage).length] = new Array(Math.floor(a / 2)).fill([[],[]])
      a = Math.floor(a / 2)
      if (isOdd)
        a = a + 1;
     stage = {...stage,... genMatches(a, match, stage)};
     return stage;
    }
    

    【讨论】:

      【解决方案3】:

      这将为您在淘汰赛中的每个BYE 以及BYES 的数量提供一个位置。 如果你想要第一轮的比赛次数

      no.of matches in first round = (teamcount - byecount)/2
      
      total matches= teamcount -1
      

      const tournamentArray = teamsCount => {
        let totalMatches = teamsCount - 1;
      	let byeCount = 0;
      	let matchStructure = [];
      	let log2 = Math.log2(teamsCount);
      	let floorValue = Math.floor(log2);
      	if (log2 > floorValue) {
              let tempPowerHolder = Math.pow(2, floorValue + 1);
      		let matches = [];
      		byeCount = tempPowerHolder - teamsCount;
      		teamsCount = tempPowerHolder / 2;
      		for (let i = 0; i < teamsCount; ++i) {
      			matches.push([]);
      		}
      		matchStructure.push(matches);
      	}
      
      	while (teamsCount > 1) {
      		teamsCount = (teamsCount + 1) >> 1;
      		let matches = [];
      		for (let i = 0; i < teamsCount; ++i) {
      			matches.push([]);
      		}
      		matchStructure.push(matches);
      	}
      
      	return {
      		byeCount,
          totalMatches,
      		matchStructure
      	};
      };
      
      console.log(tournamentArray(55))

      【讨论】:

        【解决方案4】:

         function matches(TotalTeams) {
                let teams = TotalTeams;
                let matches = [];
                let extraTeam = 0;
        
                while(teams > 1 ){
                    if(teams % 2 === 1){
                        teams = ((teams-1)/2);
                        extraTeam  = extraTeam + 1
                        matches.push(teams);
                    }
                    else{
                        teams = ((teams)/2);
                        matches.push(teams);
                    }
                    if(teams === 1){
                        const add = (a, b) => a + b;
                        const totalMatches = matches.reduce(add);
                        return (totalMatches + extraTeam)
                        
                    }
                   
                }
            }  
            
        document.getElementById("33").innerHTML = matches(33);
        document.getElementById("64").innerHTML = matches(64);
        document.getElementById("69").innerHTML = matches(69);
        document.getElementById("82").innerHTML = matches(82);
        document.getElementById("98").innerHTML = matches(98);
           
        33 teams will play <span id="33"></span> matches<br/>
        64 teams will play <span id="64"></span> matches<br/>
        69 teams will play <span id="69"></span> matches<br/>
        82 teams will play <span id="82"></span> matches<br/>
        98 teams will play <span id="98"></span> matches<br/>

        【讨论】:

          猜你喜欢
          • 2022-01-22
          • 1970-01-01
          • 1970-01-01
          • 2013-04-10
          • 2013-12-18
          • 1970-01-01
          • 2014-01-25
          • 2013-02-12
          • 2015-05-28
          相关资源
          最近更新 更多