赞
踩
某个开源社区希望将最近热度比较高的开源项目出一个榜单,推荐给社区里面的开发者。
对于每个开源项目,开发者可以进行关注(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’)。
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; } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。