循环赛赛算法的C# [英] Round Robin Tournament algorithm in C#

查看:138
本文介绍了循环赛赛算法的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦,实现这个小循环赛项目。我尝试做的是生成的游戏preVIEW日历

I am having some trouble to achieve this little round robin project. What i try to do is to generate a preview calendar of games

那么我想输出;

第1天: 球队1比2队; 球队3比队4; 团队5VS队6;

day 1: Team 1 vs Team 2; Team 3 vs Team 4; Team 5vs Team 6;

2天 1队森林狼队4; 团队6森林狼队3; 2队森林狼队5;

day 2 Team 1 vs Team 4; Team 6 vs Team 3; Team 2 vs Team 5;

直到锦标赛结束;

下面是code我已经走到这一步,但我有麻烦让一队固定的,而阵列的其余部分旋转...:

Here is the code i've got so far but i'm having trouble to let the first team fixed while the rest of the array rotates...:

static void Main(string[] args)
   {
        string[] ListTeam = new string[] {"Equipe1", "Equipe2", "Equipe3", "Equipe4", "Equipe5", "Equipe6"};
        IList<Match> ListMatch = new List<Match>();
        it NumberOfDays = (ListTeam.Count()-1);
        int y = 2;

        for (int i = 1; i <= NumberOfDays; i++)
        {
            Console.WriteLine("\nDay {0} : \n",i);
            Console.WriteLine(ListTeam[0].ToString() + " VS " + ListTeam[i].ToString());

            for (y =ListTeam.Count(); y>0 ; y--)
            {
                Console.WriteLine(ListTeam[y].ToString() + " VS " + ListTeam[y+1].ToString());
                y++;
            }

        }
    }

编辑:我发现了一个<一个href="http://www.developpez.net/forums/d267356-2/c-cpp/cpp/generation-calendrier-football/#post1700042"相对=nofollow>在java中code样品,但我不能把它翻译...

I found a code sample in java but i cant translate it...

推荐答案

这应该很容易使用模运算做:

This should be easy enough to do using modular arithmetic:

更新2:(如所承诺的正确算法)

UPDATE 2: (As promised correct algorithm)

public void ListMatches(List<string> ListTeam)
{
    if (ListTeam.Count % 2 != 0)
    {
        ListTeam.Add("Bye");
    }

    int numDays = (numTeams - 1);
    int halfSize = numTeams / 2;

    List<string> teams = new List<string>();

    teams.AddRange(ListTeam.Skip(halfSize).Take(halfSize));
    teams.AddRange(ListTeam.Skip(1).Take(halfSize -1).ToArray().Reverse());

    int teamsSize = teams.Count;

    for (int day = 0; day < numDays; day++)
    {
        Console.WriteLine("Day {0}", (day + 1));

        int teamIdx = day % teamsSize;

        Console.WriteLine("{0} vs {1}", teams[teamIdx], ListTeam[0]);

        for (int idx = 1; idx < halfSize; idx++)
        {   			
            int firstTeam = (day + idx) % teamsSize;
            int secondTeam = (day  + teamsSize - idx) % teamsSize;
            Console.WriteLine("{0} vs {1}", teams[firstTeam], teams[secondTeam]);
        }
    }
}

这将打印每一天的队比赛。

which would print each day's team matches.

让我很快地试图解释算法如何工作的:

Let me quickly try to explain how the algorithm works:

我注意到,因为我们正在旋转的所有球队,除了第一个,如果我们把所有的球队在一个数组除了第一个,那么我们应该读出从使用索引阵列抵消了一线队的基础上,一天,做模运算正确环绕。在实践中,我们将治疗该数组作为无限重复在两个方向上,我们将逐步滑动我们看来向右(或向左)。

I noticed that since we are rotating all the teams except the first one, if we put all the teams in an array except the first one, then we should just read off the first team from that array using index offset based on the day and doing modular arithmetic to wrap around correctly. In practice we would be treating that array as infinitely repeating in both directions and we would be sliding our view incrementally to right (or to the left).

还有一个障碍,但是,这是事实,我们必须订购队在此正常工作一个非常特殊的方式。否则,我们没有得到正确的旋转。正因为如此,我们需要在一个非常特殊的方式读取匹配的二队也是如此。

There is one snag, however, and that is the fact that we have to order the teams in a very particular way for this to work correctly. Otherwise, we do not get the correct rotation. Because of this we need to read of the matching second team in a very peculiar way as well.

的正确方法prepare你的名单如下:

The correct way to prepare your list is as follows:

  • 不要把一线队(组#1)在列表中。
  • 取的球队名单上半程,并把它们在列表的前面。
  • 在第一个列表中的一半,扭转它,并把它们在列表中(但不是球队#1)。

现在,要读出列表中选择正确的方法是如下:

Now, the correct way to read off the list is as follow:

  • 在每一天,增加你正在寻找由 1 的第一个指数。
  • 对于您在该位置看到了一线队,匹配队以小组第一。
  • 对于列表中的下一个队((日+ IDX)%numDays ),我们通常会使用由团队数量的一半抵消队与之匹敌减1(减1,因为我们处理的第一场比赛我们自己)。然而,由于我们的列表的第二一半是通过恢复ppared $ P $,我们需要以匹配列表中的恢复第二半偏移。更简单的方法做的是观察到,在这等同于匹配相同的索引,但在列表的末尾。鉴于目前偏移量为(日+(numDays - IDX))%numDays
  • For each day, increment the first index you are looking at by 1.
  • For the first team that you see at that location, match that team with Team#1.
  • For the next team in the list ((day + idx) % numDays), we would normally match it with the team that is offset by half the number of teams minus 1 (minus 1 because we dealt with the first match ourselves). However, since the second half of our list was prepared by reverting, we need to match that offset in the reverted second half of the list. A simpler way to do is to observe that in this is equivalent to matching the same index but from the end of the list. Given the current day offset that is (day + (numDays - idx)) % numDays.

更新3:我并不快乐,我的解决方案涉及这样令人费解的选择,匹配,扭转数组中的元素。之后我在想什么我的解决方案涉及我意识到,我得挂了大约保持球队的顺序给出。然而,这不是必需的,人们可以通过不关心的初始顺序获得不同的,但同样有效的时间表。所有的事情就是选择算法我描述了我的解释的第二部分。

UPDATE 3: I was not happy that my solution involved such convoluted selection, matching, reversing of array elements. After I got thinking about what my solution involved I realized that I was too hung up about keep the order of the teams as given. However, that is not a requirement and one can get a different but equally valid schedule by not caring about the initial ordering. All that matters is the selection algorithm I describe in the second part of my explanation.

这样可以简化如下:

teams.AddRange(ListTeam.Skip(halfSize).Take(halfSize));
teams.AddRange(ListTeam.Skip(1).Take(halfSize -1).ToArray().Reverse());

teams.AddRange(ListTeam); // Copy all the elements.
teams.RemoveAt(0); // To exclude the first team.

这篇关于循环赛赛算法的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆