按逻辑排序的锦标赛装置

时间:2017-04-06 11:25:49

标签: java algorithm sorting multimap tournament

我有即将到来的国际足联锦标赛,我写了一个打印出可能的对决的程序。问题是它没有按逻辑排序,这意味着一些玩家必须玩5-6连续游戏,而其他玩家必须等待6场比赛。我想得到以下结果:

player 1 - player 2
player 3 - player 4
player 5 - player 6
player 1 - player 3
player 2 - player 4

等等。这就是我现在所拥有的:

public class Fifa {

public static void main(String[] args) {
    String[] players= {"Jens", "Dane", "Keppens", "Roel", "John", "Onslo", "JonasDB", "Bellon", "Sander"};
    String[] players2 = {"Jens", "Dane", "Keppens", "Roel", "John", "Onslo", "JonasDB", "Bellon", "Sander"};


    Multimap<String, String> fixtures = LinkedHashMultimap.create();

    for(int i = 0; i < players.length; i++){
        for (int j = 0; j < players.length; j++){
            if(!players[i].equals(players2[j])) {
                if(!fixtures.containsKey(players2[j]))
                fixtures.put(players[i], players2[j]);
            }
        }
    }

    for(Map.Entry map : fixtures.entries()){
        String key = map.getKey().toString();
        Object value = map.getValue();
        System.out.println(key + " - " + value);
    }

但这是打印出来的:

Jens - Dane
Jens - Keppens
Jens - Roel
Jens - John
Jens - Onslo
Jens - JonasDB
Jens - Bellon
Jens - Sander
Dane - Keppens
Dane - Roel
Dane - John
Dane - Onslo
Dane - JonasDB
Dane - Bellon
Dane - Sander
Keppens - Roel
Keppens - John
Keppens - Onslo
Keppens - JonasDB
Keppens - Bellon
Keppens - Sander
Roel - John
Roel - Onslo
Roel - JonasDB
Roel - Bellon
Roel - Sander
John - Onslo
John - JonasDB
John - Bellon
John - Sander
Onslo - JonasDB
Onslo - Bellon
Onslo - Sander
JonasDB - Bellon
JonasDB - Sander
Bellon - Sander

我使用的是Multimap,因为我需要多个具有相同值的键。

1 个答案:

答案 0 :(得分:2)

一种简单的方法是循环距离,所以我们首先输出距离1,然后是2,然后是3等的所有匹配。

基本版本:

for(int dist = 1; dist < players.length; dist++)
for(int i = 0; i + dist < players.length; i++)
    System.out.println(players[i] + " - " + players[i+dist]);

这会按以下顺序进行匹配:(按距离分组)

0 - 1, 1 - 2, 2 - 3, 3 - 4, 4 - 5, 5 - 6, 
0 - 2, 1 - 3, 2 - 4, 3 - 5, 4 - 6, 
0 - 3, 1 - 4, 2 - 5, 3 - 6, 
0 - 4, 1 - 5, 2 - 6, 
0 - 5, 1 - 6, 
0 - 6, 

如果你想避免第一行中每个人连续玩2场比赛的场景,你可以把它分开并用奇数和偶数分开:

for(int i = 0; i < players.length-1; i+=2)
    System.out.println(players[i] + " - " + players[i+1]);
for(int i = 1; i < players.length-1; i+=2)
    System.out.println(players[i] + " - " + players[i+1]);

for(int dist = 2; dist < players.length; dist++)
for(int i = 0; i + dist < players.length; i++)
    System.out.println(players[i] + " - " + players[i+dist]);

按顺序给出了匹配:

0 - 1, 2 - 3, 4 - 5, 
1 - 2, 3 - 4, 5 - 6, 
0 - 2, 1 - 3, 2 - 4, 3 - 5, 4 - 6, 
0 - 3, 1 - 4, 2 - 5, 3 - 6, 
0 - 4, 1 - 5, 2 - 6, 
0 - 5, 1 - 6, 
0 - 6, 

对此的一种变化是环绕并且只绕过一半的距离(特殊情况是为了避免distance = length/2行重复匹配偶数大小的数组。)

for(int i = 0; i < players.length; i+=2)
    System.out.println(players[i] + " - " + players[(i+1)%players.length]);
for(int i = 1; i < players.length; i+=2)
    System.out.println(players[i] + " - " + players[(i+1)%players.length]);

for(int dist = 2; dist < (players.length+1)/2; dist++)
for(int i = 0; i < players.length; i++)
    System.out.println(players[i] + " - " + players[(i+dist)%players.length]);

if (players.length % 2 == 0)
    for(int i = 0; i < players.length/2; i++)
        System.out.println(players[i] + " - " + players[i+players.length/2]);

比赛将如下所示:

0 - 1, 2 - 3, 4 - 5, 6 - 0, 
1 - 2, 3 - 4, 5 - 6, 
0 - 2, 1 - 3, 2 - 4, 3 - 5, 4 - 6, 5 - 0, 6 - 1, 
0 - 3, 1 - 4, 2 - 5, 3 - 6, 4 - 0, 5 - 1, 6 - 2,