Java代码:篮球比赛球员分组算法
import java.util.*;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); int[] abilities = new int[n]; for (int i = 0; i < n; i++) { abilities[i] = scanner.nextInt(); }
StringBuilder result = new StringBuilder();
int[] teamA = new int[n];
int[] teamB = new int[n];
int index = 0;
while (index < n) {
int maxIndex = findMaxIndex(abilities);
teamA[maxIndex] = 1;
for (int i = maxIndex - m; i <= maxIndex + m; i++) {
if (i >= 0 && i < n && teamA[i] == 0 && teamB[i] == 0) {
teamA[i] = 1;
}
}
index++;
if (index >= n) {
break;
}
maxIndex = findMaxIndex(abilities);
teamB[maxIndex] = 1;
for (int i = maxIndex - m; i <= maxIndex + m; i++) {
if (i >= 0 && i < n && teamA[i] == 0 && teamB[i] == 0) {
teamB[i] = 1;
}
}
index++;
}
for (int i = 0; i < n; i++) {
if (teamA[i] == 1) {
result.append('A');
} else {
result.append('B');
}
}
System.out.println(result.toString());
}
private static int findMaxIndex(int[] array) {
int maxIndex = 0;
int maxValue = Integer.MIN_VALUE;
for (int i = 0; i < array.length; i++) {
if (array[i] > maxValue) {
maxIndex = i;
maxValue = array[i];
}
}
array[maxIndex] = Integer.MIN_VALUE;
return maxIndex;
}
}
原文地址: https://www.cveoy.top/t/topic/byes 著作权归作者所有。请勿转载和采集!