题目描述:
某个开源社区Q希望将最近热度比较高的开源项目出一个榜单,推荐给社区里面的开发者。对于每个开源项目,开发者可以进行关注(watch)、收藏(star)、fork、提issue、提交合并请求(MR)等。数据库里面统计了每个开源项日Q关注、收藏、fork、issue、MR的数量,开源项目的热度根据这5个维度的加权求和进行排序。H = Wwatch X#t watch 9 + Wstar X#star +Wfork X#fork +Wissue X#issue +mr X#mr H表示热度值,Wwatch. Wstar. WforkWissue、Wmr分别表示5个统计维度的权重,#watch、#star、#fork、#issue、#mr分别表示5个统计维度的统计值。榜单按照热度值降序排序,对于热度值相等的,按照项目名字转换为全小写字母后的字典序排序(a,’b',c..…'x,V,2),输入描述:
第一行输入为N,表示开源项目的个数,0<N<=100。
第二行输入为权重值列表,一共5个整型值,分别对应关注、收藏、fork、issue、MR的权重,权重取值0<W<=50。第三行开始接下来的N行为开源项目的统计维度,每一行的格式为:name nr watch nr star nr fork nr issue nr mr其中name为开源项目的名字,由英文字母组成,长度<=50其余5个整型值分别为该开源项日关注、收藏、fork、issue、MR的数量,数量取值0<nr<=1000.输出描述:
按照热度降序,输出开源项目的名字,对于热度值相等的,按照项目名字转换为全小写字母后的字典序排序('a'>'b'>'c'>...>'x'>'y'>'z' )。
示例1
输入:
4
8 6 2 8 6
camila 66 70 46 158 80victoria 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=2784victoria: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
根据热度值降序,得到结果。
代码
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int num=in.nextInt();//开源项目数
int[] ws=new int[5];//权重集合
for (int i = 0; i < 5; i++) {
ws[i]=in.nextInt();
}
//map集合存放开源项目名以及热度
Map<String,Integer> map =new HashMap<>();
in.nextLine();
for (int i = 0; i < num; i++) {
String[] strings=in.nextLine().split(" ");//空格拆分
String str=strings[0];
int[] w=new int[5];
w[0]=Integer.valueOf(strings[1]);
w[1]=Integer.valueOf(strings[2]);
w[2]=Integer.valueOf(strings[3]);
w[3]=Integer.valueOf(strings[4]);
w[4]=Integer.valueOf(strings[5]);
Integer integer=f(ws,w);//计算热度
map.put(str,integer);//加入map集合
}
//map集合排序
List<Map.Entry<String,Integer>> list=new ArrayList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
if(o1.getValue()== o2.getValue())
return o2.getKey().compareTo(o1.getKey());
return o2.getValue()- o1.getValue();
}
});
for (Map.Entry<String, Integer> i:list) {
System.out.println(i.getKey());
}
}
//计算热度
public static int f(int[] ws,int[] w){
int sum=0;
for (int i = 0; i < 5; i++) {
sum+=ws[i]*w[i];
}
return sum;
}
}