题目:双循环赛制每两只球队都会各比一次,赢了积3分,平手各积1分,输了不积分。现最多有26支队伍,用a,b,c…表示,统计所有球队的积分并按照从高到低排序,如果两支队伍的积分相同,则按球队的字典序排序。队伍之间用“,”分隔,输入以空行结束。
仅ac40%,有ac答案再更新到这
测试用例1:
输入:
a-b 3:0
b-a 1-1
输出:
a 4,b 1
解释:a球队胜利积3分,a与b平各积1分,a得4分b得1分
测试用例2:
输入:
a-b 3:0
a-c 2:1
b-a 1:1
c-a 0:1
b-c 4:3
c-b 2:2
输出:
a 10,b 5,c 1
import java.util.*;
/**
* @author
* @create 2021-03-31 19:01
*/
public class Test1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// int num = scanner.nextInt();
int num = 0;
// scanner.nextLine();
// StringBuilder in = new StringBuilder();
String[] in = new String[26];
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
if (s.isEmpty()){//空行结束输入
break;
}
// System.out.println(s);//
in[num] = s;
num++;
}
System.out.println(Arrays.toString(in));
// HashMap<Character, Integer> hashMap = new HashMap<>();
// int diff = 0;
int[] res = new int[26];
for (String s : in){//取出每一个字符串
if (s != "" && s != null){
if (s.charAt(4) > s.charAt(6)){//如果前面球队得分高
res[s.charAt(0) - 97] +=3;//加三分
}else if (s.charAt(4) == s.charAt(6)){
res[s.charAt(0) - 97] +=1;//相等加1分
res[s.charAt(2) - 97] +=1;
}else {
res[s.charAt(2) - 97] +=3;
}
}
}
System.out.println(Arrays.toString(res));
int newlen = 0;
for (int i = 0; i < 26; i++) {
if (res[i] == 0){
newlen = i;
break;
}
}
int[] newres = new int[newlen];
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < newlen; i++) {
newres[i] = res[i];
map.put((char)(i+97), newres[i]);//将结果存进hashmap
}
// for (Map.Entry<Character,Integer> entry : map.entrySet()){
// String r = "key = " + entry.getKey() + ",value = " + entry.getValue();
// System.out.println(r);
// }
List<Map.Entry<Character, Integer>> entryList = new ArrayList<>();
//将hashmap的entryset放进arraylist中按values进行从低到高排序
entryList.addAll(map.entrySet());
Collections.sort(entryList, new Comparator<Map.Entry<Character, Integer>>() {
@Override
public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
return -o1.getValue().compareTo(o2.getValue());
}
});
StringBuilder finalres = new StringBuilder();
for (Map.Entry<Character, Integer> entry : entryList){
finalres.append(entry.getKey()).append(" ").append(entry.getValue()).append(",");
}//去掉最后一个“,”
System.out.println(finalres.substring(0,finalres.length()-1));
}
}