我正在尝试在我的程序中实现一个括号(使用C#/.NET MVC),我试图找出一些算法.
例如,我有一个这样的括号,有8个条目(A,B,C,D,E,F,G,H)

我想弄清楚是否有算法的方法
根据条目数量,找到每轮游戏数量
根据条目数量,对于特定游戏#,下一轮相应的游戏#是什么?
例如,在这种情况下,对于8个条目,示例是:
我还想过将这些信息存储在一个表中,但它似乎有点矫枉过正,因为它永远不会改变,但无论如何它仍然存在:

任何帮助将不胜感激!
干杯,
院长
问题第一部分的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)
引用完全回答第一个问题的@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)
| 归档时间: |
|
| 查看次数: |
6526 次 |
| 最近记录: |