首页 > 解决方案 > 如何将此 PHP 转换为 C# 代码?

问题描述

我正在尝试在我的 C# asp.net 核心应用程序中创建一个锦标赛括号系统。我发现这个锦标赛支架放置算法帖子和 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;
        }

标签: c#php

解决方案


基本上是一样的array_push

例子

$var[] = "element 1";
$var[] = "element 2";

print_r($var);

将输出 Array ( [0] => element 1 [1] => element 2 )

如果你想在 c# 中做同样的事情,我认为你必须使用myList.add("element")


推荐阅读