洗牌两支球队在联赛中的名单

Shuffle list of two teams facing each other in a league

我将如何改组相互面对的团队列表,让它们永远不会相互面对两次?

我试过了。但是看看这个,它只会为 team1 生成一次对决。这意味着第一队将面对其他球队一次,所有其他球队将面对两次(主场、客场)。这有两个问题。 1. The should only face each other once (to able to repeat the number of rounds x 次) 2. 1 队不会得到与其他队伍相同的对局。

Example league
1-2
1-3 etc.

But team 2 will end up having this matchup (skipping team 1)
2-3
2-4 etc.

Team 3 will have
3-2 <---- Team 3 will here face team 2 again.
3-4 etc.

public List<Team[]> generateShuffledPairs(List<Team> teams) {
//Will add a dummy team if there are odd number of teams in the league      
listTeam = validateTeams(teams);
        int n = listTeam.size();
        for (int i = 0; i < n; i++) {
            for (int j = 0 + 1; j < n; j++) {
                if (i != j)
                    listTeamPairs.add(new Team[] { listTeam.get(i), listTeam.get(j) });
            }
        }
        listTeamPairs.stream().forEach(p -> System.out.println(p[0].getName() + " - " + p[1].getName()));
        Collections.shuffle(listTeamPairs, new Random());
        listTeamPairs.stream().forEach(p -> System.out.println(p[0].getName() + " - " + p[1].getName()));
        return listTeamPairs;
    }
for(int i = 0; i < teams.length; ++i)
{
    for(int j = i + 1; j < teams.length; ++j)
    {
        // paring: i <> j
    }
}

首先,你必须生成所有的对,如果你需要它们,然后随机排序。

public static List<int[]> generateShuffledPairs(int n) {
    ArrayList<int[]> pairs = new ArrayList<int[]>();
    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            pairs.add(new int[] { i, j });
        }
    }

    Collections.shuffle(pairs, new Random());
    return pairs;
}

public static void main(String[] args) {
    for (int[] pair : generateShuffledPairs(10)) {
        System.out.println("" + pair[0] + "-" + pair[1]);
    }
}