测试用例执行计划 - 华为OD统一考试(C卷)

OD统一考试(C卷)

分值: 100分

题解: Java / Python / C++

alt

题目描述

某个产品当前迭代周期内有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。

示例1

输入
5 4
1
1
2
3
5
1 2 3
1 4
3 4 5
2 3 4

输出
3
4
1
2

alt

示例2

输入
3 3
3
1
5
1 2 3
1 2 3
1 2 3

输出
1
2
3

alt

题解

这是一个简单的模拟题,要求按照测试用例的优先级和ID进行排序,优先级高的用例先执行,如果优先级相同则按照ID升序排列。

解题思路:

  1. 首先读取输入,包括特性数量n、测试用例数量m,以及每个特性的优先级和每个测试用例所覆盖的特性。
  2. 将特性优先级存储在一个映射(Map)中,以特性ID作为键,优先级作为值。
  3. 计算每个测试用例的优先级,即将测试用例覆盖的特性的优先级求和。
  4. 将测试用例的优先级和ID存储在一个二维数组或者二元组中,并按照优先级降序、ID升序的顺序进行排序。
  5. 输出排序后的测试用例ID。

Java

import java.util.*;

/**
 * @author code5bug
 */
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        // 读取输入的 n 和 m
        int n = in.nextInt(), m = in.nextInt();

        // 特性优先级 {特性id: 特性优先级}
        Map<Integer, Integer> futurePriority = new HashMap<>();
        for (int id = 1; id <= n; id++) {
            futurePriority.put(id, in.nextInt());
        }
        in.nextLine();

        // 测试用例: (测试用例优先级, 测试用例id)
        List<int[]> testCases = new ArrayList<>();
        for (int id = 1; id <= m; id++) {
            int testCasePriority = Arrays.stream(in.nextLine().split(" "))
                    .mapToInt(f -> futurePriority.get(Integer.parseInt(f))).sum();
            testCases.add(new int[]{testCasePriority, id});
        }

        // 按照测试用例优先级和id排序
        Collections.sort(testCases, (o1, o2) -> {
            if (o1[0] == o2[0]) {
                return o1[1] - o2[1];
            } else {
                return o2[0] - o1[0];
            }
        });

        // 输出结果
        for (int[] testCase : testCases) {
            System.out.println(testCase[1]);
        }
    }
}

Python

n, m = map(int, input().split())

# 特性优先级 {特性id: 特性优先级}
future_priority = {}
for id in range(1, n + 1):
    future_priority[id] = int(input())

# 测试用例: (测试用例优先级, 测试用例id)
test_cases = []
for id in range(1, m + 1):
    test_case_priority = 0
    for f in list(map(int, input().split())):
        test_case_priority += future_priority[f]
    test_cases.append((-test_case_priority, id))

# 按照测试用例优先级 和 id 排序
test_cases.sort()

for test_case_priority, id in test_cases:
    print(id)

C++

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

int main() {
    // 读取输入的 n 和 m
    int n, m;
    cin >> n >> m;

    // 特性优先级 {特性id: 特性优先级}
    map<int, int> futurePriority;
    for (int id = 1; id <= n; id++) {
        cin >> futurePriority[id];
    }

    // 测试用例: (测试用例优先级, 测试用例id)
    vector<pair<int, int>> testCases;
    for (int id = 1; id <= m; id++) {
        int testCasePriority = 0;
        int feature;
        while(cin >> feature) {
            testCasePriority += futurePriority[feature];
            if(cin.peek() != ' ') break;
        }

        testCases.push_back({-testCasePriority, id});
    }

    // 按照测试用例优先级和id排序
    sort(testCases.begin(), testCases.end());

    // 输出结果
    for (const auto& testCase : testCases) {
        cout << testCase.second << endl;
    }

    return 0;
}

🙏整理题解不易, 如果有帮助到您,请给点个赞 ‍❤️‍ 和收藏 ⭐,让更多的人看到。🙏🙏🙏

#面经##华为##春招##秋招##校招#
全部评论
python list1 = list(map(int,input().split())) num1 = list1[0] num2 = list1[1] perf_list=[] for i in range(num1): perf_list.append(int(input())) sum_list = [] for i in range(num2): sum_t = 0 t_id = list(map(int,input().split())) for j in t_id: sum_t +=perf_list[j-1] sum_list.append([i+1,sum_t]) sum_list_new = sorted(sum_list,key=lambda x:x[1],reverse=True) for i in sum_list_new: print(i[0])
点赞 回复 分享
发布于 2024-05-13 14:59 广东

相关推荐

被普调的六边形战士很高大:项目经历貌似和专业或者求职方向没大关系?
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务