参考自:https://blog.csdn.net/u011489043/article/details/80184972
大学生足协决定举办全国性的大学生足球赛,由每个学校派遣一支队伍代表该校参赛。比赛分区分为几个赛区进行,最终的总决赛中,将有不超过n支队伍参加。经过激烈的角逐,有机会参与总决赛的队伍已经决出。
协会对比赛的规则进行了调整,以便使得比赛更具有观赏性。
1. 总决赛的参赛队伍为n支,n为偶数;
2. 进入前1/2的队伍才有资格进入淘汰赛;
3. 队伍按积分排名,具体规则为:胜一场积3分;平一场积1分;负一场积0分。队伍首先按积分降序排列,积分相同按净胜球数降序排列,仍然相同的按进球数降序排列。
4. 基于上述规则,尚未出现有排名歧义的情况发生。
随着赛程的进行,目前各个队伍对战的结果已经确定了,小B负责确定进入淘汰赛的名单,她向你求助,你能帮她吗?
输入
测试数据有多组,每组测试数据的第一行为一个整数n(1=< n <=50),为参与总决赛的球队数,随后的n行为球队的名字,由不超过30个的大小写拉丁字母构成。随后的n*(n-1)/2行为赛事的开展情况,每行的格式为name1-name2 num1:num2,表示两支队伍的比分情况(1=<num1, num2<=100)。确保不会有两支队伍同名,也不会出现队伍自己通自己比赛的情况,且每场比赛仅出现一次。
输出
对每组测试数据,输出n/2行,为按字母序排列的进入淘汰赛的n/2支队伍的名单,每个名字在单独的行中输出。
样例输入
4
A
B
C
D
A-B 1:1
A-C 2:2
A-D 1:0
B-C 1:0
B-D 0:3
C-D 0:3
2
a
A
a-A 2:1
样例输出
A
D
a
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] namelist = new String[25];
int i,n;
int j = 0;
String input;
while(sc.hasNext()) {
input = sc.nextLine();
if("output".equals(input)) {
break;
}else{
n = Integer.valueOf(input);
Map<String,Team> map = new HashMap<String,Team>();
input(map,n,sc);
Team[] team = new Team[n];
i = 0;
for (String name : map.keySet()) {
if(name!=null)
team[i++] = map.get(name);
}
//排序
Arrays.sort(team, new Comparator<Team>() {
@Override
public int compare(Team o1, Team o2) {
int score = o2.getScore() - o1.getScore();
int realwin = o2.getRealwin() - o1.getRealwin();
int in = o2.getIn() - o1.getIn();
if(score!=0) return score;
else if(realwin!=0) return realwin;
else return in;
}
});
for (i=0; i < n/2; i++) {
namelist[j++] = team[i].getName();
}
System.out.println("----------");
}
}
//输出进入决赛的名单
for (String name : namelist) {
if(name!=null)
System.out.println(name);
}
}
//比赛记录录入
public static void input(Map<String,Team> map,int n,Scanner sc) {
for (int i = 0; i < n; i++) {
// if(map.get(sc.nextLine()) == null) {
map.put(sc.nextLine(), new Team());
// }
}
for(int i = 0; i < n*(n-1)/2; i++) {
String s = sc.nextLine();
String[] temp = s.split(" ");
String[] names = temp[0].split("-");
String[] ins = temp[1].split(":");
//主队
Team team1 = map.get(names[0]);
team1.setName(names[0]);
//客队
Team team2 = map.get(names[1]);
team2.setName(names[1]);
//进球数
int in1 = Integer.parseInt(ins[0]);
int in2 = Integer.parseInt(ins[1]);
//设置进球数
team1.setIn(in1 + team1.getIn());
team2.setIn(in2 + team2.getIn());
//设置得分
if(in1 > in2) {
team1.setScore(3 + team1.getScore());
team2.setScore(0 + team2.getScore());
}if(in1 == in2) {
team1.setRealwin(in1 - in2 + team1.getRealwin());
team2.setRealwin(in2 - in1 + team2.getRealwin());
}else {
team1.setScore(0 + team1.getScore());
team2.setScore(3 + team2.getScore());
}
map.put(names[0], team1);
map.put(names[1], team2);
}
}