重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
可以。
创新互联公司专注于企业营销型网站、网站重做改版、岳西网站定制设计、自适应品牌网站建设、成都h5网站建设、成都商城网站开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为岳西等各大城市提供网站开发制作服务。
参加比赛选手代码的主类名必须是Main,否则会被判为无效代码。
注意不要使用package语句。
如果代码中引用了类库,在提交时必须将import语句中与程序的其他部分同时提交,只允许使用Java自带的类库。
以前的!
====代码====
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 HL 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) {
Match 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);
ListListString[] 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("天: ");
ListString[] 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 MapString, ListString[] playerMacth ;
/**球员人名*/
private String[] players ;
/**最终每天的比赛分布集合*/
private ListListString[] result;
/**所有可选的比赛*/
private ListListString[] allMatchData[];
/**每天的比赛*/
ListString[] dayMatch ;
/**一个随机对象*/
private Random r;
/**当前运算到的天数*/
private int nowDay;
public Cris(int n){
this.n = n;
playerMacth = new HashMapString, ListString[]();;
result = new ArrayListListString[]();
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);
//存放每个球员要进行的比赛的集合
ListString[] matchList = new LinkedListString[]();
//球员赛事集合
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 ListString[]
*/
private ListString[] getNowDayMatch(){
if (allMatchData[nowDay] == null) {
setAllMatchNowDay();
}
if (! allMatchData[nowDay].isEmpty()) {
int index = r.nextInt(allMatchData[nowDay].size());
ListString[] 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--;
//获得前一天的比赛
ListString[] 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.随机一个比赛
ListString[] 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 LinkedListListString[]();
int n = 1;//1号运动员
//此运动员未进行的比赛
ListString[] matchList = playerMacth.get(String.valueOf(n));
for(String[] match : matchList){
ListString[] selectedList = new ArrayListString[]();
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,ListString[] 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;
}
//此运动员未进行的比赛
ListString[] matchList = playerMacth.get(player);
//此运动员可选择的比赛
ListString[] canDoList = new ArrayListString[]();
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){
ListString[] 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 ListString[]
*/
private ListString[] copyArrayList(ListString[] list){
ListString[] newList = new ArrayListString[]();
for(String[] s : list){
newList.add(s);
}
return newList;
}
public ListListString[] getResult() {
return result;
}
}
程序运行截图:
1.单循环赛,是所有参加比赛的队均能相遇一次
2.运动员类Sportsman
3.运动项目类SportsEvents
4.运动场地类PlayingField
5.测试类
import java.util.ArrayList;
public class aa {
String a,b,c;
public static void main(String[] args) {
String[] op = { "x", "y", "z" };
ArrayListaaarrayList=new ArrayListaa();
for (int i = 0; i 3; i++)
for (int j = 0; j 3; j++)
for (int k = 0; k 3; k++) {
aa a=new aa(op[i],op[j],op[k]);
if(!a.a.equals(a.b)!a.b.equals(a.c)!a.a.equals("x")
!a.c.equals("x")!a.c.equals("z")){
arrayList.add(a);
}
}
for(Object a:arrayList){
System.out.println(a);
}
}
public aa(String a, String b, String c) {
super();
this.a = a;
this.b = b;
this.c = c;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "a的对手是"+a+","+"b的对手是"+b+","+"c的对手是"+c+"\n";
}
}
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String[] args) {
final String[] str = { "a-b 1:1", "a-c 2:1", "b-c 0:1" };
final MapString, Integer map = new HashMapString, Integer();
for (String s : str) {
final String[] temp = s.split(" ");
final String[] names = temp[0].split("-");
final String[] counts = temp[1].split(":");
for (int i = 0; i 2; i++) {
String name = names[i];
int count = Integer.parseInt(counts[i]);
if (map.containsKey(name)) {
map.put(name, map.get(name) + count);
} else {
map.put(name, count);
}
}
}
final ListTeam teams = new LinkedListTeam();
for (String name : map.keySet()) {
teams.add(new Team(name, map.get(name)));
}
Collections.sort(teams);
System.out.println("队伍 : 分");
for (Team t : teams) {
System.out.println(t.getName() + " : " + t.getCount());
}
}
}
class Team implements ComparableTeam {
private String name;
private int count;
public Team(String name, int count) {
this.name = name;
this.count = count;
}
public void addCount(int count) {
this.count = this.count + count;
}
public String getName() {
return name;
}
public int getCount() {
return count;
}
public int compareTo(Team o) {
return o.getCount() - this.count;
}
}