0

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

假设我们有一个 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 的数组,但我不知道如何在每轮内生成必要数量的比赛。

4

4 回答 4

2

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

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 次方的团队计数。它确实会为轮空轮生成一个插槽(当团队数量为奇数时)。

于 2013-03-13T18:00:42.183 回答
1

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

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;
}
于 2019-08-20T11:16:41.170 回答
1

这将为您提供一个位置BYE以及 KnockOut Tournament 中的数量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))

于 2019-11-23T16:45:25.767 回答
0

 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/>

于 2019-10-11T07:48:30.867 回答