锦标赛支架算法(NCAA等)

Dea*_*ean 12 c# algorithm

我正在尝试在我的程序中实现一个括号(使用C#/.NET MVC),我试图找出一些算法.

例如,我有一个这样的括号,有8个条目(A,B,C,D,E,F,G,H)

支架示例

我想弄清楚是否有算法的方法

  1. 根据条目数量,找到每轮游戏数量

  2. 根据条目数量,对于特定游戏#,下一轮相应的游戏#是什么?

例如,在这种情况下,对于8个条目,示例是:

  1. 第1轮比赛共有4场比赛.第2轮,第2轮比赛.第3轮,第1轮比赛
  2. 第1轮中的第2场对应第2轮中的第5场比赛.

我还想过将这些信息存储在一个表中,但它似乎有点矫枉过正,因为它永远不会改变,但无论如何它仍然存在:

在此输入图像描述

任何帮助将不胜感激!

干杯,

院长

Yuc*_*uck 6

问题第一部分的C#代码:

// N = Initial Team Count
// R = Zero-Based Round #
// Games = (N / (2 ^ R)) / 2
public double GamesPerRound(int totalTeams, int currentRound) {
    var result = (totalTeams / Math.Pow(2, currentRound)) / 2;

    // Happens if you exceed the maximum possible rounds given number of teams
    if (result < 1.0F) throw new InvalidOperationException();

    return result;
}
Run Code Online (Sandbox Code Playgroud)

解决第(2)部分的下一步是知道给定回合的最小游戏数.一个直观的方法是通过for循环,但可能有一个更好的方法:

var totalTeams = 8;
var selectedRound = 2;
var firstGame = 1;
// If we start with round 1, this doesn't execute and firstGame remains at 1
for (var currentRound = 1; currentRound < selectedRound; currentRound++) {
    var gamesPerRound = GamesPerRound(totalTeams, currentRound);
    firstGame += gamesPerRound;
}
Run Code Online (Sandbox Code Playgroud)

  • 在上面的部分(1)中,R更简单,因为公式只是:var result = totalTeams/Math.Pow(2,currentRound); (3认同)

Die*_*ego 5

引用完全回答第一个问题的@Yuck.

问题第一部分的C#代码:

// N = Initial Team Count
// R = Zero-Based Round #
// Games = (N / (2 ^ R)) / 2
public double GamesPerRound(int totalTeams, int currentRound) {
    var result = (totalTeams / Math.Pow(2, currentRound)) / 2;

    // Happens if you exceed the maximum possible rounds given number of teams
    if (result < 1.0F) throw new InvalidOperationException();

    return result;
}
Run Code Online (Sandbox Code Playgroud)

继续讨论第二个问题:

//G = current game.
//T = total teams
//Next round game = (T / 2) + RoundedUp(G / 2)
//i. e.: G = 2, T = 8
       //Next round game = (8 / 2) + RoundedUp(2 / 2) = 5
public int NextGame(int totalTeams, int currentGame) {
    return (totalTeams / 2) + (int)Math.Ceiling((double)currentGame / 2);
}
Run Code Online (Sandbox Code Playgroud)