两个乒乓球队进行比赛名单编程题.

11年前
 /**   * 两个乒乓球队进行比赛,各出三人。   * 甲队为a,b,c三人,乙队为x,y,z三人。   * 已抽签决定比赛名单。有人向队员打听比赛的名单。   * a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。   */      import java.util.ArrayList;  import java.util.List;  import java.util.Map;  import java.util.Map.Entry;  import java.util.TreeMap;    /**   * The Class Race.   * @author 灬猜想灬      * @since 2012-10-13   */  public class Race  {      /** The team A. A队成员列表. */      private final List<String> teamA = new ArrayList<String>();        /** The team B. B队成员列表. */      private final List<String> teamB = new ArrayList<String>();        /** The race map. 初始比赛选手对照表. */      private final Map<String, List<String>> raceMap = new TreeMap<String, List<String>>();        /** The result map. 最终比赛选手对照表. */      private final Map<String, String> resultMap = new TreeMap<String, String>();        /** The prefered rule. 预定的比赛规则. */      private final List<String> preRule = new ArrayList<String>();        /**       * The main method.       *        * @param args the arguments       */      public static void main(final String[] args)      {          final Race t = new Race();          t.calcRaceRule();          t.adjustRaceMap();          System.out.println(t.resultMap);      }        /**       * Instantiates a new Race.       *        * @since 2012-10-13       * @author 灬猜想灬       */      public Race()      {          initTeam(teamA, "a", "b", "c");          initTeam(teamB, "x", "y", "z");          initRaceMap();          preRule.add("a!=x");          preRule.add("c!=x");          preRule.add("c!=z");      }        /**       * Initial the team.<br>       * 初始化队成员列表.       *        * @param racers the teamers       */      public void initTeam(final List<String> team, final String... racers)      {          for (final String racer : racers)          {              team.add(racer);          }      }        /**       * Initial the race map.<br>       * 初始化初始比赛选手对照表.<br>       * 默认A队的选手可能会对上B队的每一个人.       */      public void initRaceMap()      {          for (final String racer : teamA)          {              raceMap.put(racer, new ArrayList<String>(teamB));          }      }        /**       * Calc race rule.<br>       * 根据预定规则计算选手对照表.       */      private void calcRaceRule()      {          for (final String rule : preRule)          {              final String a = String.valueOf(rule.charAt(0));              final String b = String.valueOf(rule.charAt(rule.length() - 1));              final List<String> rivals = raceMap.get(a);              if (null != rivals && rivals.contains(b))              {                  rivals.remove(b);              }          }      }        /**       * Adjust race map.<br>       * 调整最终的比赛对照表.       */      public void adjustRaceMap()      {          // 当结果选手对照表的人数和参赛队伍中的人数相等时才结束调整.          while (resultMap.size() != teamA.size())          {              for (final Entry<String, List<String>> entry : raceMap.entrySet())              {                  final String racerA = entry.getKey();                  final List<String> racerBs = entry.getValue();                  // 结果对照表中已包含选手A时跳过.                  if (resultMap.containsKey(racerA))                  {                      continue;                  }                  // 从选手A的所有对手中移除所有已经在结果对照表中的选手.                  racerBs.removeAll(resultMap.values());                  // 如果选手A的对手列表中只剩下一个选手B,则将这个组合加入结果对照表中.                  if (1 == racerBs.size())                  {                      resultMap.put(racerA, racerBs.get(0));                  }              }          }      }  }