以前的!
====代码====
package com;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 网球赛
* @author Cris
* @ClassName: Match
* @Version
* @ModifiedBy
* @Copyright @ 2010 H&L Technology Inc.
* @date 2011-5-24 上午11:29:39
* @description
*/
public class Match implements ActionListener{
private JFrame f;
private JTextField tf;
private JPanel container;
private JList myList ;
private JLabel message;
private JButton b;
/**
* @author Cris
* @title: main
* @date 2011-5-23 上午11:07:27
* @param args void
*/
public static void main(String[] args) {
32313133353236313431303231363533e59b9ee7ad9431333335303438Match t = new Match();
t.go();
}
public void go(){
f = new JFrame("比赛");
f.setLocation(100, 200);
f.setSize(600,500);
container = new JPanel(new BorderLayout());
f.setContentPane(container);
JPanel title = new JPanel(new FlowLayout());
tf = new JTextField(10);
title.add(tf);
b = new JButton("确定");
b.addActionListener(this);
title.add(b);
message = new JLabel();
title.add(message);
container.add(title,BorderLayout.NORTH);
myList = new JList();
container.add(myList,BorderLayout.CENTER);
f.setVisible(true);
}
/**
* 显示比赛结果
* @author Cris
* @title: show
* @date 2011-5-24 上午11:31:05
* @param n void
*/
private void show(int n){
Cris c = new Cris(n);
List> result = c.getResult();
String[] str = new String[result.size()];
for (int i = 0; i < result.size(); i++) {
StringBuilder sb = new StringBuilder();
sb.append("第").append(i+1).append("天: ");
List matchList = result.get(i);
for(String[] match : matchList){
sb.append("[").append(match[0]).append("-").append(match[1]).append("] ");
}
str[i] = sb.toString();
}
myList.setListData(str);
message.setText("");
f.setVisible(true);
}
/**
* 清空数据
* @author Cris
* @title: clear
* @date 2011-5-24 上午11:30:36 void
*/
private void clear(){
String[] str = {""};
myList.setListData(str);
message.setText("");
}
public void actionPerformed(ActionEvent e) {
clear();
String s = tf.getText();
if (s == null || "".equals(s = s.trim()) ) {
message.setText("请输入一个偶数");
}
try {
int n = Integer.parseInt(s);
if (n <= 0 || n % 2 != 0) {
message.setText("请输入一个偶数");
}else{
show(n);
}
} catch (Exception ex) {
message.setText("请输入一个偶数");
}
f.setVisible(true);
}
}
第二个类:
package com;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class Cris {
/**选手数量*/
private int n ;
/**各个运动员还没进行的比赛 key:球员名称,value:需进行的比赛集合*/
private Map> playerMacth ;
/**球员人名*/
private String[] players ;
/**最终每天的比赛分布集合*/
private List> result;
/**所有可选的比赛*/
private List> allMatchData[];
/**每天的比赛*/
List dayMatch ;
/**一个随机对象*/
private Random r;
/**当前运算到的天数*/
private int nowDay;
public Cris(int n){
this.n = n;
playerMacth = new HashMap>();;
result = new ArrayList>();
allMatchData = new LinkedList[n-1];
r = new Random();
init();
go();
}
/**
* 初始化球员
* @author Cris
* @title: init
* @date 2011-5-23 下午04:22:32 void
*/
private void init(){
//定义球员人名,以数字代表,从1开始
players = new String[n];
for (int i = 0; i < players.length; i++) {
//定义球员的名字
players[i] = String.valueOf(i+1);
//存放每个球员要进行的比赛的集合
List matchList = new LinkedList();
//球员赛事集合
playerMacth.put(players[i], matchList);
//球员集合,不变
//playerListConst.add(players[i]);
}
//定义每个人的比赛
for (int i = 0; i < players.length -1; i++) {
for (int j = i+1; j < players.length ; j++) {
String[] match = {players[i],players[j]};//定义一个比赛
//把此赛事添加到相应球员需比赛集合中
playerMacth.get(players[i]).add(match);
playerMacth.get(players[j]).add(match);
}
}
}
/**
* 随机获得一个当前天数可进行的比赛组合,并把此比赛组合从总集合中移除
* 如果当前天数无比赛,则先获得比赛,再随机
* @author Cris
* @title: getNowDayMatch
* @date 2011-5-24 上午09:48:23
* @return List
*/
private List getNowDayMatch(){
if (allMatchData[nowDay] == null) {
setAllMatchNowDay();
}
if (! allMatchData[nowDay].isEmpty()) {
int index = r.nextInt(allMatchData[nowDay].size());
List list = allMatchData[nowDay].get(index);
allMatchData[nowDay].remove(index);
return list;
}else {
return null;
}
}
/**
* 取消前一天的比赛
* @author Cris
* @title: cancelYesterday
* @date 2011-5-24 上午09:56:12 void
*/
private void cancelYesterday(){
//清空当前天数的可选比赛组合
allMatchData[nowDay] = null ;
//时间恢复到前一天
nowDay--;
//获得前一天的比赛
List matchList = result.get(nowDay);
//取消前一天的比赛
result.remove(nowDay);
//恢复每个运动员未进行的比赛
for(String[] match : matchList){
playerMacth.get(match[0]).add(match);
playerMacth.get(match[1]).add(match);
}
}
public void go(){
//循环比赛天数
for ( nowDay = 0; nowDay < n -1; ) {
//1.随机一个比赛
List matchList = getNowDayMatch();
//无比赛组合 ,返回上一天 取消上一天的比赛,时间回退到前一天,跳过此次循环
if (matchList == null) {
cancelYesterday();
continue;
}
//3遍历比赛 去除运动员 去除运动员的未进行比赛
for(String[] match : matchList){
playerMacth.get(match[0]).remove(match);
playerMacth.get(match[1]).remove(match);
}
result.add(matchList);
nowDay++;
}
}
/**
* 从1号队员开始,获得当前天数所有可组合的赛事
* @author Cris
* @title: setAllMatchNowDay
* @date 2011-5-24 上午09:42:51 void
*/
private void setAllMatchNowDay(){
allMatchData[nowDay] = new LinkedList>();
int n = 1;//1号运动员
//此运动员未进行的比赛
List matchList = playerMacth.get(String.valueOf(n));
for(String[] match : matchList){
List selectedList = new ArrayList();
selectedList.add(match);
doDetAllMatchNowDay(n+1, selectedList);
}
}
/**
* 今天比赛的所有组合
* @author Cris
* @title: doDetAllMatchNowDay
* @date 2011-5-24 上午09:46:05
* @param playerNum
* @param selectedList void
*/
private void doDetAllMatchNowDay(int playerNum,List selectedList){
//运动员名称
String player = String.valueOf(playerNum);
boolean flag = true;
//判断此运动员是不是已被选过
for(String[] match : selectedList){
if (match[0].equals(player) || match[1].equals(player)) {
flag = false;
break;
}
}
//已被选过
if ( flag == false) {
//最后一个人员
if (playerNum == n) {
allMatchData[nowDay].add(selectedList);
}else{
doDetAllMatchNowDay(playerNum+1, selectedList);
}
return;
}
//此运动员未进行的比赛
List matchList = playerMacth.get(player);
//此运动员可选择的比赛
List canDoList = new ArrayList();
for(String[] match : matchList){
flag = true;
for(String[] selected : selectedList){
if(match[0].equals(player)){
if(match[1].equals(selected[0])){
flag = false;
}else if(match[1].equals(selected[1])){
flag = false;
}
}else{
if(match[0].equals(selected[0])){
flag = false;
}else if(match[0].equals(selected[1])){
flag = false;
}
}
}
if (flag) {
canDoList.add(match);
}
}
//遍历此运动员可选择的比赛
for(String[] match : canDoList){
List newList = copyArrayList(selectedList);
//记录当前比赛
newList.add(match);
//最后一个人员
if (playerNum == n) {
allMatchData[nowDay].add(newList);
}else{
doDetAllMatchNowDay(playerNum+1, newList);
}
}
}
/**
* 复制集合
* @author Cris
* @title: copyArrayList
* @date 2011-5-24 上午09:24:44
* @param list
* @return List
*/
private List copyArrayList(List list){
List newList = new ArrayList();
for(String[] s : list){
newList.add(s);
}
return newList;
}
public List> getResult() {
return result;
}
}