算法题-测试用例执行计划
题目描述
某个产品当前迭代周期内有N个特性(F1, F2, ..., FN)需要进行覆盖测试,每个特性都被评估了对应的优先级,特性使用其ID作为下标进行标识。
设计了M个测试用例(T1, T2,...,TM),每个用例对应了一个覆盖特性的集合,测试用例使用其ID作为下标进行标识,测试用例的优先级定义为其覆盖的特性的优先级之和。
在开展测试之前,需要制定测试用例的执行顺序,规则为:优先级大的用例先执行,如果存在优先级相同的用例,用例ID小的先执行。
输入描述
第一行输入为N和M,N表示特性的数量,M表示测试用例的数量。
之后N行表示特性ID=1到特性ID=N的优先级。
再接下来M行表示测试用例ID=1到测试用例ID=M关联的特性的ID的列表。
输出描述
按照执行顺序(优先级从大到小)输出测试用例的ID,每行一个ID。
测试用例
输入
5 4
1
1
2
3
5
1 2 3
1 4
3 4 5
2 3 4
输出
3
4
1
2
测试用例
输入
3 3
3
1
5
1 2 3
1 2 3
1 2 3
输出
1
2
3
代码
public class TestSort {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int N = in.nextInt();
int[] priority = new int[N];
int M = in.nextInt();
int[] sum = new int[M];
Arrays.fill(sum, 0);
for (int i=0; i< N ;i++) {
priority[i] = in.nextInt();
}
in.nextLine();
for (int i=0; i< M; i++) {
String testFeature = in.nextLine();
String[] features = testFeature.split(" ");
for (int j=0; j< features.length; j++) {
int feature = Integer.parseInt(features[j]);
sum[i]+= priority[feature-1];
}
}
int[] result = new int[M];
for (int i=0; i<M ;i++) {
result[i] = i;
}
for (int i=0; i<M-1; i++) {
int max = i;
for (int j=i+1; j<M; j++) {
if (sum[result[j]]>sum[result[max]]) {
max = j;
}
}
if (max != i) {
int tmp = result[i];
result[i] = result[max];
result[max] = tmp;
}
}
for (int i=0; i<M; i++) {
System.out.println(result[i]+1);
}
}
}
}

浙公网安备 33010602011771号