2013-03-06 34 views
4

我不知道如何讓他們一次又一次地玩對方。例如,這裏是團隊的樣本(我會在後面加得分,尋找主要是邏輯的幫助):我將如何讓一組球隊一次只玩一次?

class MyTeams { 
    String teamName; 
    int wins; 
    int losses; 
} 

public class BasketallSeason { 

    public static void main(String[] args) { 

     MyTeams aoTeams[] = new MyTeams[9]; 

     aoTeams[0].teamName = "Utah"; 
     aoTeams[1].teamName = "USC"; 
     aoTeams[2].teamName = "Saint Mary's"; 
     aoTeams[3].teamName = "Oregon"; 
     aoTeams[4].teamName = "San Diego"; 
     aoTeams[5].teamName = "San Francisco"; 
     aoTeams[6].teamName = "UCLA"; 
     aoTeams[7].teamName = "Washington"; 
     aoTeams[8].teamName = "Loyola"; 
    } 
} 

回答

3

我實際上花了一些時間並編寫了答案。這是一個非常有趣的問題。有很多的方式來解決這個問題:

  1. 花式迭代
  2. 遞歸
  3. 迭代,而突然離開球隊
  4. 使用一個單獨的結構,以「標記」團隊爲已處理

此圖片可能會幫助您:

0 1 2 3 
0 - A A A 
1 B - A A 
2 B B - A 
3 B B B - 

矩陣的X軸和Y軸顯示您想要的答案。組A或組B將給你答案。請注意,您不希望團隊按矩陣中的破折號表示自己玩遊戲。下面是使用迭代3個選項:

public class BBall { 

public static void main(String args[]) { 

    List<String> teams = new ArrayList<String>(); 
    teams.add("Boston"); 
    teams.add("LA"); 
    teams.add("New York"); 
    teams.add("Chicago"); 
    teams.add("Dallas"); 

    // This option might be a little easier to read. 
    int index1 = 0; 
    System.out.println("Easy to read:"); 
    for (String team1 : teams) { 
     index1++; 
     for (int index2 = index1; index2 < teams.size(); ++index2) { 
      System.out.println(team1 + " plays " + teams.get(index2)); 
     } 
    } 
    System.out.println("This is set A:"); 
    for (int x = 1; x < teams.size(); x++) { 
     for (int y = x - 1; y >= 0; y--) { 
      System.out.println(teams.get(x) + " plays " + teams.get(y)); 
     } 
    } 
    System.out.println("This is set B:"); 
    for (int x = 0; x < teams.size() - 1; x++) { 
     for (int y = x + 1; y < teams.size(); y++) { 
      System.out.println(teams.get(x) + " plays " + teams.get(y)); 
     } 
    } 
} 
} 

輸出:

易於閱讀:

Boston plays LA 
    Boston plays New York 
    Boston plays Chicago 
    Boston plays Dallas 
    LA plays New York 
    LA plays Chicago 
    LA plays Dallas 
    New York plays Chicago 
    New York plays Dallas 
    Chicago plays Dallas 

此設置答:

LA plays Boston 
    New York plays LA 
    New York plays Boston 
    Chicago plays New York 
    Chicago plays LA 
    Chicago plays Boston 
    Dallas plays Chicago 
    Dallas plays New York 
    Dallas plays LA 
    Dallas plays Boston 

此設置B:

Boston plays LA 
    Boston plays New York 
    Boston plays Chicago 
    Boston plays Dallas 
    LA plays New York 
    LA plays Chicago 
    LA plays Dallas 
    New York plays Chicago 
    New York plays Dallas 
    Chicago plays Dallas 
5

這是一個基本算法爲你

  • 就拿陣列
  • 的第一隊
  • 讓球隊打出陣列中的其他球隊
  • 從球員陣列中刪除該球隊,或者以其他方式將該球隊標記爲已經打滿了每個人,這樣就可以忽略它。
  • 重複
1

定義兩個嵌套循環for每個循環,在你的團隊。通常情況下,每個球隊都會打兩次,每個球隊都會打一次。

您不希望如此,因此將內部for循環的循環變量初始化爲外加for循環的循環變量的當前值一。這可以確保每個團隊組合都重複一次,而團隊不會自行發揮作用。

然後調用您在內部for循環內部具有的任何遊戲邏輯。

0

在你的班級myTeams中維護一個java.util.BitSet參考,並用等於團隊數量的數字對它進行實例化。默認情況下,所有值均爲,未設置(false)。

當一隊嘗試與aoTeams中的其他球隊進行比賽時,請檢查BitSet中對手數組索引處的值。如果沒有設置,讓它播放,設置該索引的值。對其他團隊也一樣。

示例實現可能類似於:

class Team { 

    String teamName; 
    int wins; 
    int losses; 
    BitSet playRecord; 

    public Team(String name, int size) { 
     this.teamName = name; 
     playRecord = new BitSet(size); 
    } 

    public boolean hasPlayed(int index) { 
     return playRecord.get(index); 
    } 

    public void finishedPlaying(int index) { 
     playRecord.set(index); 
    } 

} 

這是你如何使用它:

public static void main(String[] args) { 
    int size = 9; 
    Team aoTeams[] = new Team[size]; 
    aoTeams[0] = new Team("Utah", size); 
    aoTeams[1] = new Team("USC", size); 
    play(aoTeams, 0, 1); 
} 

private static void play(Team[] teams, int indexA, int indexB) { 
    if (teams[indexA].hasPlayed(indexB)) { 
     // Teams have already played together 
    } else { 
     // Teams playing for the first time 
     teams[indexA].finishedPlaying(indexB); 
     teams[indexB].finishedPlaying(indexA); 
    } 
} 
0

創建類myTeams一個布爾陣列(比如alreadyPlayed),具有全是假的初始化它們在構造函數中,當team1使用team2時,設置alreadyPassed [2] = true(在team1的對象中)並在team2的對象中設置alreadyPassed [1] = true。

1

循環,像這樣:

for(int i = 0; i < aoTeams.length - 1; i++) { 
    for(int j = i+1; j < aoTeams.length; j++) { 
     aoTeams[i].playAgainst(aoTeams[j]); 
    } 
} 
1

應該是正確的:

for (int i = aoTeams.length-1; i >= 0; i--) 
    for (int j = i-1; j >= 0; j--) 
     System.out.println(aoTeams[j].teamName + " : "+ aoTeams[i].teamName); 

不要忘記在第一次captial信使用Java類和駝峯總是寫場開始以較低的字母;-)

0

一個簡單的嵌套循環。迭代這兩個列表併發出對,從第一個循環索引開始第二個循環。