当前位置:   article > 正文

华为od机试C卷-开源项目热度榜单_开源项目热榜

开源项目热榜

我的喵

1、题目描述

某个开源社区希望将最近热度比较高的开源项目出一个榜单,推荐给社区里面的开发者。
对于每个开源项目,开发者可以进行关注(watch)、收藏(star)、fork、提issue、提交合并请求(MR)等。
数据库里面统计了每个开源项目关注、收藏、fork、issue、MR的数量,开源项目的热度根据这5个维度的加权求和进行排序。
H = (Wwatch * #watch) + (Wstar * #star) + (Wfork * #fork) + (Wissue * #issue) + (Wmr * #mr)
H表示热度值
Wwatch、Wstar、Wfork、Wissue、Wmr分别表示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’)。

2、解法

import java.util.Comparator;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt(); //读取项目数
        
        int[] weights = new int[5]; //读取权重
        for (int i = 0; i < weights.length; i++) {
            weights[i] = in.nextInt();
        }

        Map<String, String> hotMap = new TreeMap<>(new MyComparator());

        for (int i = 0; i < n; i++) {
            int tempHot = 0;
            String tempName = in.next();
            for (int j = 0; j < weights.length; j++) {
                tempHot += weights[j] * in.nextInt();
            }
            hotMap.put(tempName+"_"+tempHot, tempName);
        }

        for (Map.Entry<String, String> prjEntry : hotMap.entrySet()) {
            System.out.println(prjEntry.getValue());
        }
    }

    public static int compareStr(String str1, String str2) {
        str1 = str1.toLowerCase();
        str2 = str2.toLowerCase();

        int len = str1.length() > str2.length() ? str2.length() : str1.length();

        int i = 0;
        for (; i < len; i++) {
            if (str1.charAt(i) < str2.charAt(i)) {
                return 1;
            } else if (str1.charAt(i) == str2.charAt(i)) {
                continue;
            } else if (str1.charAt(i) > str2.charAt(i)) {
                return -1;
            }
        }

        return 0;
    }

    public static class MyComparator implements Comparator<String> {
        @Override
        public int compare(String k1, String k2) {
            String k1Name = k1.split("_")[0];
            int k1Hot = Integer.parseInt(k1.split("_")[1]);

            String k2Name = k2.split("_")[0];
            int k2Hot = Integer.parseInt(k2.split("_")[1]);

            if (k1Hot < k2Hot) {
                return 1;
            } else if (k1Hot == k2Hot) {
                if (compareStr(k1Name, k2Name) > 0) {
                    return 1;
                } else if (compareStr(k1Name, k2Name) == 0) {
                    return 1;
                } else if (compareStr(k1Name, k2Name) < 0) {
                    return -1;
                }
            } else {
                return -1;
            }

            return 0;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/431895?site
推荐阅读
相关标签
  

闽ICP备14008679号