【独家OD2023C卷真题】20天拿下华为OD笔试【排序】2023C-开源项目热榜【欧弟算法】全网注释最详细分类最全的华为OD真题题解

题目描述与示例

题目描述

某个开源社区希望将最近热度比较高的开源项目出一个榜单,推荐给社区里面的开发者。

对于每个开源项目,开发者可以进行关注(watch)、收藏(star)、fork、提 issue、提交合并请求(MR)等。

数据库里面统计了每个开源项目关注、收藏、fork、issue、MR 的数量,开源项目的热度根据这 5 个维度的加权求和进行排序。

H = W w a t c h ∗ # w a t c h + W s t a r ∗ # s t a r + W f o r k ∗ # f o r k + W i s s u r e ∗ # i s s u r e + W m r ∗ # m r H = W_{watch} * \#watch +W_{star} * \#star+W_{fork} * \#fork+W_{issure} * \#issure+W_{mr} * \#mr H=Wwatch#watch+Wstar#star+Wfork#fork+Wissure#issure+Wmr#mr

  • H H H:表示热度值;
  • W w a t c h , W s t a r , W f o r k , W i s s u r e , W m r W_{watch},W_{star} ,W_{fork},W_{issure} ,W_{mr} WwatchWstarWforkWissureWmr:分别表示 5 个统计维度的权重;
  • # w a t c h , # s t a r , # f o r k , # i s s u r e , # m r \#watch ,\#star,\#fork,\#issure,\#mr #watch#star#fork#issure#mr:分别表示 5 个统计维度的统计值。

榜单按照热度值降序排序,对于热度值相等的,按照项目名字转换为全小写字母后的字典序排序。

输入描述

第一行输入为 N,表示开源项目的个数,0 < N < 100

第二行输入为权重值列表,一共 5 个整型值,分别对应关注、收藏、fork、issue、MR 的权重,权重取值 0 < M ≤ 50

第三行开始接下来的 N 行为开源项目的统计维度,每一行的格式为:

name nr_watch nr_star nr_fork nr_issue nr_mr

其中 name 为开源项目的名字,由英文字母组成,长度 ≤50,其余 5 个整型值分别为该开源项目关注、收藏、fork、issue、MR 的数量,数量取值 0 < nr ≤ 1000

输出描述

按照热度降序,输出开源项目的名字,对于热度值相等的,按照项目名字转换为全小写字母后的字典序排序

示例一

输入

4
8 6 2 8 6
camila 66 70 46 158 80
victoria 94 76 86 189 211
anthony 29 17 83 21 48
emily 53 97 1 19 218

输出

victoria
camila
emily
anthony

说明

排序热度值计算:

camila: 668 + 706 + 462 + 1588 + 80*6 = 2784

victoria: 948 + 766 + 862 + 1898 + 211*6 = 4158

anthony: 298 + 176 + 832 + 218 + 48*6 = 956

emily: 538 + 976 + 12 + 198 + 218*6 = 2468

根据热度值降序,得到结果。

示例二

输入

5
5 6 6 1 2
camila 13 88 46 26 169
grace 64 38 87 23 103
lucas 91 79 98 154 79
leo 29 27 36 43 178
ava 29 27 36 43 178

输出

lucas
grace
camila
ava
leo

说明

排序热度值计算:

camila: 135 + 886 + 466 + 261 + 169*2 = 1233

grace: 645 + 386 + 876 + 231 + 103*2 = 1299

lucas: 915 + 796 + 986 + 1541 + 79*2 = 1829

leo: 295 + 276 + 366 + 431 + 178*2 = 922

ava: 295 + 276 + 366 + 431 + 178*2 = 922

根据热度值降序,对于 leo 和 ava,热度值相等,按照字典序,ava 排在 leo 前面,得到结果。

解题思路

本题考察编程基础和阅读理解。对于每一个项目名字,我们都能够根据公式计算得到一个总热度。先根据热度进行降序排序,相同热度的项目再根据全小写的项目名字进行字典序升序排序。

代码

Python

# 题目:【排序】2023C-开源项目热榜
# 分值:100
# 作者:许老师-闭着眼睛学数理化
# 算法:模拟/排序
# 代码看不懂的地方,请直接在群上提问


n = int(input())
# 输入五个维度的权重
w1, w2, w3, w4, w5 = map(int, input().split())

lst = list()
for _ in range(n):
    # 输入项目名字以及该项目各个维度的统计值
    name, a1, a2, a3, a4, a5 = input().split()
    # 根据公式计算结果
    total = w1*int(a1) + w2*int(a2) + w3*int(a3) + w4*int(a4) + w5*int(a5)
    # 将项目名字和结果,以二元组的形式存入lst种
    lst.append((name, total))

# 先根据每一个项目的total值进行降序排序
# 再根据name的全小写形式升序排序
lst.sort(key = lambda x: (-x[1], x[0].lower()))

# 逐行输出
for name, total in lst:
    print(name)

Java

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();

        int w1, w2, w3, w4, w5;
        w1 = scanner.nextInt();
        w2 = scanner.nextInt();
        w3 = scanner.nextInt();
        w4 = scanner.nextInt();
        w5 = scanner.nextInt();
        scanner.nextLine();

        List<Map.Entry<String, Integer>> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String[] input = scanner.nextLine().split(" ");
            String name = input[0];
            int a1 = Integer.parseInt(input[1]);
            int a2 = Integer.parseInt(input[2]);
            int a3 = Integer.parseInt(input[3]);
            int a4 = Integer.parseInt(input[4]);
            int a5 = Integer.parseInt(input[5]);

            int total = w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4 + w5 * a5;
            list.add(new AbstractMap.SimpleEntry<>(name, total));
        }

        Collections.sort(list, (a, b) -> {
            if (!a.getValue().equals(b.getValue())) {
                return Integer.compare(b.getValue(), a.getValue());
            }
            return a.getKey().toLowerCase().compareTo(b.getKey().toLowerCase());
        });

        for (Map.Entry<String, Integer> entry : list) {
            System.out.println(entry.getKey());
        }
    }
}

C++

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

using namespace std;

int main() {
    int n;
    cin >> n;

    int w1, w2, w3, w4, w5;
    cin >> w1 >> w2 >> w3 >> w4 >> w5;

    vector<pair<string, int>> lst;
    for (int i = 0; i < n; i++) {
        string name;
        int a1, a2, a3, a4, a5;
        cin >> name >> a1 >> a2 >> a3 >> a4 >> a5;

        int total = w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4 + w5 * a5;
        lst.push_back(make_pair(name, total));
    }

    sort(lst.begin(), lst.end(), [](const pair<string, int> &a, const pair<string, int> &b) {
        if (a.second != b.second) {
            return b.second < a.second;
        }
        return a.first < b.first;
    });

    for (const auto &p : lst) {
        cout << p.first << endl;
    }

    return 0;
}

时空复杂度

时间复杂度:O(NlogN)。排序所需时间复杂度。

空间复杂度:O(N)


华为OD算法/大厂面试高频题算法练习冲刺训练

  • 华为OD算法/大厂面试高频题算法冲刺训练目前开始常态化报名!目前已服务100+同学成功上岸!

  • 课程讲师为全网50w+粉丝编程博主@吴师兄学算法 以及小红书头部编程博主@闭着眼睛学数理化

  • 每期人数维持在20人内,保证能够最大限度地满足到每一个同学的需求,达到和1v1同样的学习效果!

  • 60+天陪伴式学习,40+直播课时,300+动画图解视频,300+LeetCode经典题,200+华为OD真题/大厂真题,还有简历修改、模拟面试、专属HR对接将为你解锁

  • 可上全网独家的欧弟OJ系统练习华子OD、大厂真题

  • 可查看链接 大厂真题汇总 & OD真题汇总(持续更新)

  • 绿色聊天软件戳 od1336了解更多

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值