看网上这道题用java写的几乎没有,就分享一下自己的写法
输入格式:
输入的第一行给出两个正整数N和M(≤224,因为世界上共有224个国家和地区),分别是参与排名的国家和地区的总个数、以及前来咨询的国家的个数。为简单起见,我们把国家从0 ~ N−1编号。之后有N行输入,第i行给出编号为i−1的国家的金牌数、奖牌数、国民人口数(单位为百万),数字均为[0,1000]区间内的整数,用空格分隔。最后面一行给出M个前来咨询的国家的编号,用空格分隔。
输出格式:
在一行里顺序输出前来咨询的国家的排名:计算方式编号
。其排名按照对该国家最有利的方式计算;计算方式编号为:金牌榜=1,奖牌榜=2,国民人均金牌榜=3,国民人均奖牌榜=4。输出间以空格分隔,输出结尾不能有多余空格。若某国在不同排名方式下有相同名次,则输出编号最小的计算方式。
思路:这道题我就是直接调用JAVA容器的API实现,注意一个很细节的点就是,如果两个国家计算排名的数值参数是相等的那么它的排名是并列的,举个例子:1(5个金牌)2(5个金牌)3(3个金牌)三个人如果按金牌数量来排名的话,则1,2都是第一名,3是第三名。这个点很容易忽略。
AC代码如下:
import java.util.*;
public class ptaOlympicGames {
public static LinkedHashMap<Integer,Person>hm=new LinkedHashMap<>();//key是国家的编号,value是构造的Person类
public static ArrayList<Map.Entry<Integer,Person>>s1=new ArrayList<>();//完成最后的操作
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<Integer>a=new ArrayList<>();//用来存储要来咨询的国家编号
int n=sc.nextInt();
int m=sc.nextInt();
for(int i=0;i<n;i++){
hm.put(i, new Person(sc.nextInt(),sc.nextInt(),sc.nextInt()));
}
for(int i=0;i<m;i++){
a.add(sc.nextInt());
}
for(Map.Entry x:hm.entrySet()){
s1.add(x);
}
sort1();//对应四种排法
sort2();
sort3();
sort4();
for(int i=0;i<s1.size();i++){
s1.get(i).getValue().getTemp();//对他们的temp,temp2赋值
}
for(int j=0;j<a.size();j++){
int i=a.get(j);
if(j!=a.size()-1)
System.out.print(s1.get(i).getValue().temp+":"+s1.get(i).getValue().temp2+" ");
else
System.out.print(s1.get(i).getValue().temp+":"+s1.get(i).getValue().temp2);
}
}
public static void sort1(){
ArrayList<Map.Entry<Integer,Person>>s=new ArrayList<>();
for(Map.Entry x:hm.entrySet()){
s.add(x);
}
Collections.sort(s,(a,b)->b.getValue().a1-a.getValue().a1);
double same=s.get(0).getValue().a1;
s.get(0).getValue().f[0]=0;
int te=0;
for(int i=1;i<s.size();i++){
if(s.get(i).getValue().a1==same) {
s.get(i).getValue().f[0] = te;
}
else{
s.get(i).getValue().f[0]=i;
same=s.get(i).getValue().a1;
te=i;
}
}
}
public static void sort2(){
ArrayList<Map.Entry<Integer,Person>>s=new ArrayList<>();
for(Map.Entry x:hm.entrySet()){
s.add(x);
}
Collections.sort(s,(a,b)->b.getValue().a2-a.getValue().a2);
double same=s.get(0).getValue().a2;
s.get(0).getValue().f[1]=0;
int te=0;
for(int i=1;i<s.size();i++){
if(s.get(i).getValue().a2==same) {
s.get(i).getValue().f[1] = te;
}
else{
s.get(i).getValue().f[1]=i;
same=s.get(i).getValue().a2;
te=i;
}
}
}
public static void sort3(){
ArrayList<Map.Entry<Integer,Person>>s=new ArrayList<>();
for(Map.Entry x:hm.entrySet()){
s.add(x);
}
Collections.sort(s,(a,b)->{
if(b.getValue().a3-a.getValue().a3>0)
return 1;
else if(b.getValue().a3-a.getValue().a3==0)
return 0;
else
return -1;
});
double same=s.get(0).getValue().a3;
s.get(0).getValue().f[2]=0;
int te=0;
for(int i=1;i<s.size();i++){
if(s.get(i).getValue().a3==same) {
s.get(i).getValue().f[2] = te;
}
else{
s.get(i).getValue().f[2]=i;
same=s.get(i).getValue().a3;
te=i;
}
}
}
public static void sort4(){
ArrayList<Map.Entry<Integer,Person>>s=new ArrayList<>();
for(Map.Entry x:hm.entrySet()){
s.add(x);
}
Collections.sort(s,(a,b)->{
if(b.getValue().a4-a.getValue().a4>0)
return 1;
else if(b.getValue().a4-a.getValue().a4==0)
return 0;
else
return -1;
});
double same=s.get(0).getValue().a4;
s.get(0).getValue().f[3]=0;
int te=0;
for(int i=1;i<s.size();i++){
if(s.get(i).getValue().a4==same) {
s.get(i).getValue().f[3] = te;
}
else{
s.get(i).getValue().f[3]=i;
same=s.get(i).getValue().a4;
te=i;
}
}
}
}
class Person{//定义一个国家类
int a1,a2;//分别对应四种排法
double a3,a4;
int []f=new int[4];
int temp;//最大排名
int temp2;//对应的排名方式
Person(int a,int b,int c){
a1=a;
a2=b;
a3=a/(double)c;
a4=b/(double)c;
}
public void getTemp(){//用于获得temp,temp2
int min=100000;
int t=0;
for(int i=0;i<f.length;i++){
if(f[i]<min){
min=f[i];
t=i;
}
}
temp=min+1;
temp2=t+1;
}
}
输入样例:
4 4
51 100 1000
36 110 300
6 14 32
5 18 40
0 1 2 3
输出样例:
1:1 1:2 1:3 1:4