返回目录

题目描述

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

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

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

H = W(watch) x #watch + W(star) x #star + W(fork) x #fork + W(issue) x #issue + W(mr) x #mr

  • H 表示热度值
  • W(watch)、W(star)、W(fork)、W(issue)、W(mr) 分别表示5个统计维度的权重
  • watch、#star、#fork、#issue、#mr 分别表示5个统计维度的统计值

榜单按照热度值降序排序,对于热度值相等的,按照项目名字转换为全小写字母后的字典序排序('a','b','c',...,'x','y','z')。

输入描述

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

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

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

name nr\_watch nr\_start nr\_fork nr\_issue nr\_mr

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

输出描述

按照热度降序,输出开源项目的名字,对于热度值相等的,按照项目名字转换为全小写后的字典序排序('a' > 'b' > 'c' > ... > 'x' > 'y' > 'z')。

示例:

输入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:66*8 + 70*6 + 46*2 + 158*8 + 80*6 = 2784
victoria: 94*8 + 76*6 + 86*2 + 189*8 + 211*6 = 4158
anthony: 29*8 + 17*6 + 83*2 + 21*8 + 48*6 = 956
emily: 53*8 + 97*6 + 1*2 + 19*8 + 218*6 = 2468

题目解析

简单的多条件排序题。

具体逻辑请看代码实现。

Python算法源码

class Project:
    def __init__(self, name, hot):
        self.name = name  # 项目名称
        self.hot = hot    # 热度

def calculate_hotness(statistics, weights):
    hot = 0
    for i in range(5):
        hot += statistics[i] * weights[i]  # 计算项目热度
    return hot

def compare_projects(projectA, projectB):
    # 根据热度比较项目
    if projectA.hot != projectB.hot:
        return projectB.hot - projectA.hot
    else:
        # 如果热度相等,则按项目名称字母顺序排序
        return 1 if projectA.name > projectB.name else -1

if __name__ == "__main__":
    # 输入项目数量
    n = int(input("输入项目数量:"))

    # 输入权重
    weights = list(map(int, input("输入权重:").split()))

    # 输入项目详情
    projects = []
    for _ in range(n):
        name = input("输入项目名称:")
        statistics = list(map(int, input("输入统计数据:").split()))
        hot = calculate_hotness(statistics, weights)
        projects.append(Project(name, hot))

    # 根据热度和名称对项目排序
    projects.sort(key=lambda x: (x.hot, x.name), reverse=True)

    # 打印排序后的项目名称
    print("排序后的项目:")
    for project in projects:
        print(project.name)

C算法源码


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Project {
    char name[21]; // 假设项目名称最多为20个字符
    int hot; // 热度值
};

// 计算项目热度的函数
int calculate_hotness(int statistics[], int weights[]) {
    int hot = 0;
    for (int i = 0; i < 5; i++) {
        hot += statistics[i] * weights[i];
    }
    return hot;
}

// 用于比较项目的排序函数
int compare_projects(const void *a, const void *b) {
    struct Project *projectA = (struct Project *)a;
    struct Project *projectB = (struct Project *)b;
  
    // 按热度值降序排序
    if (projectA->hot != projectB->hot)
        return projectB->hot - projectA->hot;
  
    // 如果热度值相等,则按字母顺序升序排序
    return strcmp(projectA->name, projectB->name);
}

int main() {
    int n;
    scanf("%d", &n);

    int weights[5];
    for (int i = 0; i < 5; i++) {
        scanf("%d", &weights[i]);
    }

    struct Project projects[n];
    for (int i = 0; i < n; i++) {
        char name[21];
        scanf("%s", name);

        int statistics[5];
        for (int j = 0; j < 5; j++) {
            scanf("%d", &statistics[j]);
        }

        int hot = calculate_hotness(statistics, weights);
        strcpy(projects[i].name, name);
        projects[i].hot = hot;
    }

    // 对项目进行排序
    qsort(projects, n, sizeof(struct Project), compare_projects);

    // 打印排序后的项目名称
    for (int i = 0; i < n; i++) {
        printf("%s\n", projects[i].name);
    }

    return 0;
}

Java算法源码

import java.util.*;

class Project {
    String name;
    int hot;

    public Project(String name, int hot) {
        this.name = name;  // 项目名称
        this.hot = hot;    // 热度
    }
}

public class Main {
    public static int calculateHotness(int[] statistics, int[] weights) {
        int hot = 0;
        for (int i = 0; i < 5; i++) {
            hot += statistics[i] * weights[i];  // 计算项目热度
        }
        return hot;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入项目数量:");
        int n = scanner.nextInt();

        int[] weights = new int[5];
        System.out.print("输入权重:");
        for (int i = 0; i < 5; i++) {
            weights[i] = scanner.nextInt();
        }

        Project[] projects = new Project[n];
        for (int i = 0; i < n; i++) {
            System.out.print("输入项目名称:");
            String name = scanner.next();
            int[] statistics = new int[5];
            System.out.print("输入统计数据:");
            for (int j = 0; j < 5; j++) {
                statistics[j] = scanner.nextInt();
            }
            int hot = calculateHotness(statistics, weights);
            projects[i] = new Project(name, hot);
        }

        Arrays.sort(projects, new Comparator<Project>() {
            @Override
            public int compare(Project a, Project b) {
                if (a.hot != b.hot) {
                    return b.hot - a.hot;  // 根据热度比较项目
                } else {
                    return a.name.compareTo(b.name);  // 如果热度相等,则按名称字母顺序排序
                }
            }
        });

        System.out.println("排序后的项目:");
        for (Project project : projects) {
            System.out.println(project.name);
        }
    }
}
最后修改:2024 年 03 月 31 日
如果觉得我的文章对你有用,请随意赞赏