我正在尝试在我的C#asp.net核心应用程序中创建一个比赛支架系统。我发现了这篇Tournament bracket placement algorithm帖子,我需要RWC的答案,因为它还包括再见。
我遇到的问题是将这段代码翻译为c#:
<?php
define('NUMBER_OF_PARTICIPANTS', 16);
$participants = range(1,NUMBER_OF_PARTICIPANTS);
$bracket = getBracket($participants);
var_dump($bracket);
function getBracket($participants)
{
$participantsCount = count($participants);
$rounds = ceil(log($participantsCount)/log(2));
$bracketSize = pow(2, $rounds);
$requiredByes = $bracketSize - $participantsCount;
echo sprintf('Number of participants: %d<br/>%s', $participantsCount, PHP_EOL);
echo sprintf('Number of rounds: %d<br/>%s', $rounds, PHP_EOL);
echo sprintf('Bracket size: %d<br/>%s', $bracketSize, PHP_EOL);
echo sprintf('Required number of byes: %d<br/>%s', $requiredByes, PHP_EOL);
if($participantsCount < 2)
{
return array();
}
$matches = array(array(1,2));
for($round=1; $round < $rounds; $round++)
{
$roundMatches = array();
$sum = pow(2, $round + 1) + 1;
foreach($matches as $match)
{
$home = changeIntoBye($match[0], $participantsCount);
$away = changeIntoBye($sum - $match[0], $participantsCount);
$roundMatches[] = array($home, $away);
$home = changeIntoBye($sum - $match[1], $participantsCount);
$away = changeIntoBye($match[1], $participantsCount);
$roundMatches[] = array($home, $away);
}
$matches = $roundMatches;
}
return $matches;
}
function changeIntoBye($seed, $participantsCount)
{
//return $seed <= $participantsCount ? $seed : sprintf('%d (= bye)', $seed);
return $seed <= $participantsCount ? $seed : null;
}
?>
我尝试将每行PHP代码转换为C#等效代码。但是,此片段是阻止我前进的原因:
for($round=1; $round < $rounds; $round++)
{
$roundMatches = array();
$sum = pow(2, $round + 1) + 1;
foreach($matches as $match)
{
$home = changeIntoBye($match[0], $participantsCount);
$away = changeIntoBye($sum - $match[0], $participantsCount);
$roundMatches[] = array($home, $away);
$home = changeIntoBye($sum - $match[1], $participantsCount);
$away = changeIntoBye($match[1], $participantsCount);
$roundMatches[] = array($home, $away);
}
$matches = $roundMatches;
}
我不知道$roundMatches[]
想要完成什么。重新创建数组吗?它在设置指针吗?不知道。我编写的C#版本为每次比赛都给了我错误的种子编号。
public Dictionary<int, string> getBracket(Dictionary<int, string> participants)
{
Dictionary<int, string> orderedParticipants = new Dictionary<int, string>();
var count = participants.Count;
var rounds = Math.Ceiling(Math.Log(count) / Math.Log(2));
var bracketSize = Math.Pow(2, rounds);
var requiredByes = bracketSize - count;
string p = $"Number of participants: {count}";
string r = $"Number of rounds: {rounds}";
string b = $"Bracket size: {bracketSize}";
string byes = $"Required number of byes: {requiredByes}";
List<List<int>> matches = new List<List<int>>();
matches.Add(new List<int>() {1, 2});
for (int round = 1; round < rounds; round++)
{
List<int> roundMatches = new List<int>();
var sum = (int)Math.Pow(2, round + 1) + 1;
foreach (var match in matches)
{
var home = changeIntoBye(match[0], count);
var away = changeIntoBye(sum - match[0], count);
roundMatches = new List<int> {home.Value, away.Value};
//roundMatches
home = changeIntoBye(sum - match[1], count);
away = changeIntoBye(match[1], count);
}
matches.Add(roundMatches);
}
return orderedParticipants;
}
答案 0 :(得分:0)
它与array_push
示例
$var[] = "element 1";
$var[] = "element 2";
print_r($var);
将输出
Array ( [0] => element 1 [1] => element 2 )
如果您想在C#中做同样的事情,我认为您必须使用myList.add("element")