测试用例执行计划
2025华为OD机试- 华为OD上机考试 100分题型
华为OD机试真题目录点击查看: 华为OD机试真题题库目录|机考题库 + 算法考点详解
题目描述
某个产品当前迭代周期内有 N 个特性(F1,F2,...FN)需要进行覆盖测试,每个特性都被评估了对应的优先级,特性使用其 ID 作为下标进行标识。
设计了 M 个测试用例(T1,T2,...,TM),每个测试用例对应一个覆盖特性的集合,测试用例使用其 ID 作为下标进行标识,测试用例的优先级定义为其覆盖的特性的优先级之和。
在开展测试之前,需要制定测试用例的执行顺序,规则为:优先级大的用例先执行,如果存在优先级相同的用例,用例 ID 小的先执行。
输入描述
第一行输入为 N 和 M,
- N 表示特性的数量,0 < N ≤ 100
- M 表示测试用例的数量,0 < M ≤ 100
之后 N 行表示特性 ID=1 到特性 ID=N 的优先级,
再接下来 M 行表示测试用例 ID=1 到测试用例 ID=M 关联的特性的 ID 的列表。
输出描述
按照执行顺序(优先级从大到小)输出测试用例的 ID,每行一个ID。
测试用例覆盖的 ID 不重复。
用例1
输入
none
5 4
1
1
2
3
5
1 2 3
1 4
3 4 5
2 3 4
输出
none
3
4
1
2
说明
测试用例的优先级计算如下:
T1 = Pf1 + Pf2 + Pf3 = 1 + 1 + 2 = 4
T2 = Pf1 + Pf4 = 1 + 3 = 4
T3 = Pf3 + Pf4 + Pf5 = 2 + 3 + 5 = 10
T4 = Pf2 + Pf3 + Pf4 = 1 + 2 + 3 = 6
按照优先级从大到小,以及相同优先级,ID小的先执行的规则,执行顺序为T3,T4,T1,T2
用例2
输入
none
3 3
3
1
5
1 2 3
1 2 3
1 2 3
输出
none
1
2
3
说明
测试用例的优先级计算如下:
T1 = Pf1 + Pf2 + Pf3 = 3 + 1 + 5 = 9
T2 = Pf1 + Pf2 + Pf3 = 3 + 1 + 5 = 9
T3 = Pf1 + Pf2 + Pf3 = 3 + 1 + 5 = 9
每个优先级一样,按照 ID 从小到大执行,执行顺序为T1,T2,T3
题解
思路:模拟 + 自定义排序
- 使用
数组保存各个特性的优先级(由于后续测试用例中特性的序号从1开始,这里可以进行优化将对应特性优先级保存在数组{1,n}序号下) - 计算每个测试用例的总优先级,并以
{总优先级,测试用例序号}保存到结果数组中。 - 对结果数组进行自定排序,先按照总优先级降序,优先级相同按照测试用例序号升序。
- 输出结果。
c++
c++
#include<iostream>
#include<vector>
#include<string>
#include <utility>
#include <sstream>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
// 通用 切割函数 函数 将字符串str根据delimiter进行切割
vector<int> split(const string& str, const string& delimiter) {
vector<int> result;
size_t start = 0;
size_t end = str.find(delimiter);
while (end != string::npos) {
result.push_back(stoi(str.substr(start, end - start)));
start = end + delimiter.length();
end = str.find(delimiter, start);
}
// 添加最后一个部分
result.push_back(stoi(str.substr(start)));
return result;
}
int main() {
int n, m;
cin >> n >> m;
vector<int> priority(n+1, 0);
for (int i = 1; i <= n; i++) {
cin >> priority[i];
}
// 忽略空行
cin.ignore();
vector<vector<int>> testCase(m);
for (int i = 0; i < m; i++) {
string input;
getline(cin, input);
vector<int> chooseCase = split(input, " ");
int totalPriority = 0;
for (int j = 0; j < chooseCase.size(); j++) {
int index = chooseCase[j];
totalPriority += priority[index];
}
// 将序号 + 1, 因为题目测试用例序号1开始
testCase[i] = {totalPriority, i + 1};
}
// 自定义排序
sort(testCase.begin(), testCase.end(), [](const vector<int>& a, const vector<int>& b){
if (a[0] == b[0]) {
return a[1] < b[1];
}
return a[0] > b[0];
});
// 输出结果
for (int i = 0; i < m; i++) {
cout << testCase[i][1] << endl;
}
return 0;
}
JAVA
JAVA
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 读取 n 和 m
int n = sc.nextInt();
int m = sc.nextInt();
sc.nextLine(); // 吃掉换行
// priority 下标从 1 开始,每行一个
int[] priority = new int[n + 1];
for (int i = 1; i <= n; i++) {
priority[i] = sc.nextInt();
}
sc.nextLine(); // 吃掉换行
// 存储 [总优先级, 测试用例编号]
List<int[]> testCase = new ArrayList<>();
for (int i = 0; i < m; i++) {
String line = sc.nextLine().trim();
String[] parts = line.split(" ");
int totalPriority = 0;
for (String p : parts) {
int idx = Integer.parseInt(p);
totalPriority += priority[idx];
}
// 测试用例编号从 1 开始
testCase.add(new int[]{totalPriority, i + 1});
}
// 排序:优先级降序,编号升序
testCase.sort((a, b) -> {
if (a[0] == b[0]) {
return a[1] - b[1];
}
return b[0] - a[0];
});
// 输出结果
for (int[] t : testCase) {
System.out.println(t[1]);
}
}
}
Python
python
import sys
# 读取 n, m
n, m = map(int, sys.stdin.readline().split())
# priority 下标从 1 开始,每行一个
priority = [0]
for _ in range(n):
priority.append(int(sys.stdin.readline().strip()))
test_case = []
for i in range(m):
choose_case = list(map(int, sys.stdin.readline().split()))
total_priority = 0
for idx in choose_case:
total_priority += priority[idx]
test_case.append((total_priority, i + 1))
# 排序:优先级降序,编号升序
test_case.sort(key=lambda x: (-x[0], x[1]))
# 输出
for _, idx in test_case:
print(idx)
JavaScript
js
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const lines = [];
rl.on('line', line => {
lines.push(line.trim());
});
rl.on('close', () => {
let pos = 0;
const [n, m] = lines[pos++].split(' ').map(Number);
// priority 下标从 1 开始,每行一个
const priority = [0];
for (let i = 0; i < n; i++) {
priority.push(Number(lines[pos++]));
}
const testCase = [];
for (let i = 0; i < m; i++) {
const chooseCase = lines[pos++].split(' ').map(Number);
let totalPriority = 0;
for (const idx of chooseCase) {
totalPriority += priority[idx];
}
testCase.push([totalPriority, i + 1]);
}
// 排序:优先级降序,编号升序
testCase.sort((a, b) => {
if (a[0] === b[0]) return a[1] - b[1];
return b[0] - a[0];
});
// 输出
let res = '';
for (const t of testCase) {
res += t[1] + '\n';
}
console.log(res);
});
Go
go
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
)
func main() {
in := bufio.NewReader(os.Stdin)
var n, m int
fmt.Fscan(in, &n, &m)
// priority 下标从 1 开始,每行一个
priority := make([]int, n+1)
for i := 1; i <= n; i++ {
fmt.Fscan(in, &priority[i])
}
// 吃掉换行
in.ReadString('\n')
testCase := make([][2]int, m)
for i := 0; i < m; i++ {
line, _ := in.ReadString('\n')
parts := strings.Fields(line)
totalPriority := 0
for _, p := range parts {
var idx int
fmt.Sscanf(p, "%d", &idx)
totalPriority += priority[idx]
}
testCase[i] = [2]int{totalPriority, i + 1}
}
// 排序:优先级降序,编号升序
sort.Slice(testCase, func(i, j int) bool {
if testCase[i][0] == testCase[j][0] {
return testCase[i][1] < testCase[j][1]
}
return testCase[i][0] > testCase[j][0]
})
writer := bufio.NewWriter(os.Stdout)
for i := 0; i < m; i++ {
fmt.Fprintln(writer, testCase[i][1])
}
writer.Flush()
}