逻辑排序的比赛装置
Logically sorted tournament fixtures
我有一场即将举行的国际足联锦标赛,我编写了一个程序来打印出可能的对决。问题是它没有按逻辑排序,这意味着一些玩家必须连续玩 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、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,
我有一场即将举行的国际足联锦标赛,我编写了一个程序来打印出可能的对决。问题是它没有按逻辑排序,这意味着一些玩家必须连续玩 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、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,