7.23 科大讯飞笔试 第三题
import java.util.*;
public class Main {
static HashSet<String> set = new HashSet<>();
static String[] teams = {"a", "b", "c", "d", "e"};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < 5; i++) {
sb.append(scanner.nextInt());
if (i != 4) {
sb.append(",").append(" ");
}
}
sb.append("]");
HashMap<String, Integer> map = new HashMap<>();
map.put("a", 0);
map.put("b", 0);
map.put("c", 0);
map.put("d", 0);
map.put("e", 0);
competition(0, 1, map, 0);
String ans = "";
if (num == set.size()) {
ans += "yes";
} else {
ans += "no";
}
ans += " ";
if (set.contains(sb.toString())) {
ans += "yes";
} else {
ans += "no";
}
System.out.println(ans);
}
private static void competition(int i, int j, HashMap<String, Integer> map, int count) {
if (count == 10) {
Collection<Integer> values = map.values();
Integer[] array = values.toArray(new Integer[]{});
Arrays.sort(array, (o1, o2) -> o2 - o1);
set.add(Arrays.toString(array));
return;
}
String team1 = teams[i];
String team2 = teams[j];
map.put(team1, map.getOrDefault(team1, 0) + 3);
competition(j == 4 ? i + 1 : i, j == 4 ? i + 2 : j + 1, new HashMap<>(map), count + 1);
map.put(team1, map.getOrDefault(team1, 0) - 3);
map.put(team1, map.getOrDefault(team1, 0) + 1);
map.put(team2, map.getOrDefault(team2, 0) + 1);
competition(j == 4 ? i + 1 : i, j == 4 ? i + 2 : j + 1, new HashMap<>(map), count + 1);
map.put(team1, map.getOrDefault(team1, 0) - 1);
map.put(team2, map.getOrDefault(team2, 0) - 1);
map.put(team2, map.getOrDefault(team2, 0) + 3);
competition(j == 4 ? i + 1 : i, j == 4 ? i + 2 : j + 1, new HashMap<>(map), count + 1);
map.put(team2, map.getOrDefault(team2, 0) - 3);
}
}
大佬们看看,这个代码对不对?
#笔试##科大讯飞#
查看20道真题和解析