洛谷 帮贡排序
解题思路
题目描述内容过多,注意读懂,然后做几个排序即可。
代码描述
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String s1="BangZhu";
String s2="FuBangZhu";
String s3="HuFa";
String s4="ZhangLao";
String s5="TangZhu";
String s6="JingYing";
String s7="BangZhong";
int index=0;
People[] sta=new People[3];
People[] p=new People[n-3];
String name;
String identity;
String tribute;
int grade;
for(int i=0;i<n;i++) {
name=sc.next();
identity=sc.next();
tribute=sc.next();
grade=sc.nextInt();
if(i<3) {
sta[index]=new People(name, identity, tribute, grade, i);
index++;
}
else p[i-3]=new People(name, identity, tribute, grade, i);
}
Arrays.sort(p);
//进行身份转换
ArrayList<People>[] arr=new ArrayList[5];
for(int i=0;i<5;i++) {
arr[i]=new ArrayList<Main.People>();
}
index=1;
for(int i=0;i<n-3;i++) {
if(index<=2) {
p[i].identity=s3;
arr[0].add(p[i]);
}
else if(index<=6) {
p[i].identity=s4;
arr[1].add(p[i]);
}
else if(index<=13) {
p[i].identity=s5;
arr[2].add(p[i]);
}
else if(index<=38) {
p[i].identity=s6;
arr[3].add(p[i]);
}
else {
p[i].identity=s7;
arr[4].add(p[i]);
}
index++;
}
for(int i=0;i<5;i++) {
Collections.sort(arr[i],new Comparator<People>() {
public int compare(People o1,People o2) {
//等级排序
if(o1.grade!=o2.grade) return o2.grade-o1.grade;
//经验排序
return o1.Id-o2.Id;
}
});
}
//开始进行输出
for(int i=0;i<3;i++) {
System.out.println(sta[i].name+" "+sta[i].identity+" "+sta[i].grade);
}
for(int i=0;i<5;i++) {
for(People p1:arr[i]) {
System.out.println(p1.name+" "+p1.identity+" "+p1.grade);
}
}
return;
}
static public class People implements Comparable<People>{
People(String name,String identity,String tribute,int grade,int id){
this.name=name;
this.identity=identity;
this.tribute=tribute;
this.grade=grade;
this.Id=id;
}
public String name;
public String identity;
public String tribute;
public int grade;
public int Id;
@Override
public int compareTo(People o2) {
//帮贡 从大到小
if(this.tribute.length()-o2.tribute.length()!=0) {
return o2.tribute.length()-this.tribute.length();
}
return -this.tribute.compareTo(o2.tribute);
}
}
}