6

我正在开发基于循环调度算法的 Java 体育赛事。对于n团队,我想生成2(n-1)带有n/2比赛的回合。也就是每支球队必须打一场比赛,每2支球队交手两次,一次客场,一次主场。除了家庭/离开部分,我设法实现了算法。我能够生成回合,但不能在回合的后半段“交换”球队,所以他们在客场和主场比赛。

这是我到目前为止所拥有的:

public class sports {
  public static void main(String[] args) {
    //obtain the number of teams from user input
    Scanner input = new Scanner(System.in);
    System.out.print("How many teams should the fixture table have?");
    int teams = input.nextInt();
    // Generate the schedule using round robin algorithm.
    int totalRounds = (teams - 1) * 2;
    int matchesPerRound = teams / 2;
    String[][] rounds = new String[totalRounds][matchesPerRound];
    for (int round = 0; round < totalRounds; round++) {
      for (int match = 0; match < matchesPerRound; match++) {
        int home = (round + match) % (teams - 1);
        int away = (teams - 1 - match + round) % (teams - 1);
        // Last team stays in the same place
        // while the others rotate around it.
        if (match == 0) {
          away = teams - 1;
        }
        // Add one so teams are number 1 to teams
        // not 0 to teams - 1 upon display.
        rounds[round][match] = ("team " + (home + 1)
            + " plays against team " + (away + 1));
      }
    }
    // Display the rounds
    for (int i = 0; i < rounds.length; i++) {
      System.out.println("Round " + (i + 1));
      System.out.println(Arrays.asList(rounds[i]));
      System.out.println();
    }
  }
}

不要介意偶数/奇数队,现在我只对偶数队数感兴趣。

4

3 回答 3

3

为了编写 True Soft 的答案,

String roundString;
if (round < halfRoundMark) {
    roundString = ("team " + (home + 1)
            + " plays against team " + (away + 1));
} else {
    roundString = ("team " + (away + 1)
            + " plays against team " + (home + 1));
}
rounds[round][match] = roundString;

在哪里

int halfRoundMark = (totalRounds / 2);
于 2013-12-31T08:52:57.000 回答
0

你必须看看你什么时候到了半场,然后切换主客队。

于 2013-12-31T08:50:07.067 回答
0

请参阅此链接,该链接描述了“奇数团队”“偶数团队”的循环算法。

参考此链接:- https://nrich.maths.org/1443

1) 奇数队:- 对于奇数队,考虑以下公式和条件

缩写:-

TNR = Total Number Of Rounds
NOT = Number Of Teams
MPR = Match Per Round

公式:-

Total Number Of Rounds = Number Of Teams
Match Per Round = [ ( Number Of Teams - 1 ) / 2 ]

条件:-一轮必须只有一支球队,轮空(不参加比赛)。所以每一轮只有一次轮空每支球队将与另一支球队进行一轮比赛。

让我们考虑这个算法在 c# 中的实现(奇数个团队)

假设我们有 5 个团队

团队:-

Barcilona (0)
Brazil   (1)
Madrid  (2)
Colambo  (3)
Woshington DC  (4)

这里 ,

团队数量(非)= 5

总轮数 (TNR) = 5

每轮比赛 (MPR) = [ ( NOT - 1 ) / 2 ] = [ ( 5 - 1 ) /2 ] = 2

现在考虑每轮有一支球队轮空,而另一支球队与另一支球队比赛一次的链接。

现在将所有团队放在名为 Teams 的数组中

int[] teams = new int[NOT]; 
so, int[] teams = new int[5];
now teams = [0,1,2,3,4]

现在我们将在下一轮移动时将数组向左旋转1,并且团队 [0]将始终得到 By并在其他团队之间进行比赛,但问题是我们如何决定哪支球队与哪支球队比赛。所以要解决这些问题,请参考参考链接。因此,您可以确定在第 0 轮中,将与以下球队进行匹配

Teams[0] = 0 = Barcilona Gets Bye(Not played game)
Teams[1] - Teams[4] = (Brazil - Woshington DC)
Teams[2] - Teams[3] = (Madrid - Colambo)

所以,考虑以下实现

public class Teams {
    public int Team1 { get; set; }
    public int Team2 { get; set; }
}

//Dictionary< round , matches >
var matchScheduling = new Dictionary<int, List<Teams>>();

for (int round = 0; round < TNR; round++) {
    List<Teams> matches = new List<Teams>();
    int team1 = teams[0];
    int team2 = -1;
    matches.Add(new Teams() { Team1 = team1, Team2 = team2 });

    for (int match = 0; match < MPR; match++) {
        team1 = teams[match + 1];
        team2 = teams[(NOT.Length - 1) - match];
        roundTeams.Add(new Teams() { Team1 = team1, Team2 = team2 });
    }

    matchScheduling.Add(round, roundTeams);

    //Now for next round, Rotate team Array
    int length = teams.Length;
    int tmp = teams[length - 1];
    for (int i = length - 1; i > 0; i--) {
        teams[i] = teams[i - 1];
    }
    teams[0] = tmp;
}
于 2019-03-01T11:52:16.700 回答