双循环赛

时间:2013-12-31 08:39:33

标签: java schedule round-robin

我正在基于循环调度算法开发java中的运动项目。对于n支队伍,我希望用n / 2场比赛产生2(n-1)轮。也就是说每支球队都必须在一轮比赛中进行比赛,每两支球队会两次,一次一次,一次回家。我设法实现了algoritm,除了home / away部分。我能够产生回合,但是在下半场不能“交换”球队,所以他们可以在主场和主场比赛。

这是我到目前为止所做的:

import java.util.Arrays;
import java.util.Scanner;

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;
    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 个答案:

答案 0 :(得分:1)

编写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);

答案 1 :(得分:0)

你必须看到你到达半轮的时间,然后转换主队和客队。

答案 2 :(得分:0)

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

引用此链接:-https://nrich.maths.org/1443

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

缩写:-

$component.find('StyledComponent');

公式:-

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

条件:-    一轮中只能有一支球队,他们会再见(不会玩游戏)。    因此,每回合只有一个再见,并且每个团队将在    与其他团队进行回合

让我们考虑一下该算法在c#(单数组)中的实现

假设我们有5个团队

团队:-

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

在这里,

队伍数(NOT)= 5

总发球数(TNR)= 5

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

现在考虑一下每个回合中有一支球队再见的链接,而另一支球队与另一支球队进行一次回合。

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

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

现在我们将左移1步 旋转阵列,同时下一轮团队移动 [0] 总是获得并在其他团队之间进行游戏,但是问题在于我们如何确定哪个团队与哪个团队进行游戏。因此,要解决这些问题,请参考参考链接。所以你可以找出      在第0轮中,将与以下球队进行比赛

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

因此,请考虑以下实施方式

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

答案 3 :(得分:-2)

您好我必须向卖家提供产品,我已经编写了以下代码检查 算法中的算法

package RoundRobin;

import java.util.ArrayList;
import java.util.List;

public class RoundRobin {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List seller = new ArrayList();
        seller.add("a");
        seller.add("b");
        // seller.add("c");
        List products = new ArrayList();
        products.add("1");
        products.add("2");
        products.add("3");
        products.add("4");
        products.add("5");
        products.add("6");
        products.add("7");

        List SellerProducts = new ArrayList();

        int sellerLength = seller.size();
        int productLength = products.size();
        System.out.println(sellerLength + "<-selleL , prodLength->" + productLength);
        int divide = productLength / sellerLength;
        int mod = productLength % sellerLength;

        for (int sel = 1; sel <= divide; sel++) {
            for (int sCount = 0; sCount < sellerLength; sCount++) {
                // System.out.println("Scount" + sCount);
                if (sel == 1) {
                    SellerProducts.add(seller.get(sCount) + "-" + products.get(sCount));
                } else if (sel > 1) {
                    int next = sCount + ((sel * sellerLength) - sellerLength);
                    // System.out.println(next);
                    SellerProducts.add(seller.get(sCount) + "-" + products.get(next));
                }
            }
        }

        // for Remaining
        for (int p = 0
                + (sellerLength * divide), initCountForSeller = 0; p < productLength; p++, initCountForSeller++) {
            SellerProducts.add(seller.get(initCountForSeller) + "-" + products.get(p));
        }
        System.out.println("Seller : " + seller);
        System.out.println("products : " + products);
        System.out.println("seller product " + SellerProducts);
    }

}