6

ラウンド ロビン スケジューリング アルゴリズムに基づいて、Java でスポーツ トーナメントを開発しています。チームの場合、試合でラウンドnを生成したいと考えています。つまり、すべてのチームが 1 ラウンドで試合を行う必要があり、2 チームごとに 2 回 (アウェイとホームで 1 回) 対戦します。ホーム/アウェイ部分を除いて、アルゴリズムを実装することができました。ラウンドを生成することはできますが、ラウンドの後半でチームを「交換」できないため、アウェイとホームの両方でプレーします。2(n-1)n/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 に答える