4.17网易互娱

第一题字典排序怎么处理的啊,我用的是
comparaTo。要么是0,要么是过了10。咋回事。
代码在下边,麻烦大佬们帮忙看看。
package Tmp5;

import java.io.UnsupportedEncodingException;
import java.util.*;

public class Main {

    static class Football{
        private String name;
        private int points;
        private int goals_for;
        private int goals_against;
        private int goals_clean;

        Football(String name, int points, int goals_for, int goals_against, int goals_clean){
            this.name = name;
            this.points = points;
            this.goals_for = goals_for;
            this.goals_against = goals_against;
            this.goals_clean = goals_clean;
        }

        public String getName(){
            return name;
        }
        public void setName(String name){
            this.name = name;
        }

        public int getPoints(){
            return points;
        }
        public void setPoints(int i){
            this.points = i;
        }

        public int getGoals_for(){
            return goals_for;
        }
        public void setGoals_for(int i){
            this.goals_for = i;
        }

        public int getGoals_against(){
            return goals_against;
        }
        public void setGoals_against(int i){
            this.goals_against = i;
        }
        public int getGoals_clean(){
            return goals_clean;
        }
        public void setGoals_clean(int i){
            this.goals_clean = i;
        }
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
//        测试数据
        int T = Integer.parseInt(sc.nextLine());




        for (int i=0;i<T;i++){
            List<Football> footballs = new ArrayList<>();
            int finsh=6;
            int last_game=3;
            while(finsh!=0){
                String[] line = sc.nextLine().split(" ");
                footballs.add(new Football(line[0],Integer.parseInt(line[1]),Integer.parseInt(line[2]),Integer.parseInt(line[3]),Integer.parseInt(line[2])-Integer.parseInt(line[3])));
                finsh--;
            }
//            现在要修改已有的数据
            while (last_game!=0){
                String[] line = sc.nextLine().split(" ");
                String name0 = line[0];
                String name1 = line[1];
                int goals_for1 = Integer.parseInt(line[2]);
                int goals_for2 = Integer.parseInt(line[3]);
                for(Football f:footballs){
                    if(f.getName().equals(name0)){
                        f.setGoals_for(f.getGoals_for()+goals_for1);
                        f.setGoals_clean(f.getGoals_for()-f.getGoals_against());
                        if(goals_for1>goals_for2){
                            f.setPoints(f.getPoints()+3);
                        }
                        if(goals_for1==goals_for2){
                            f.setPoints(f.getPoints()+1);
                        }

                        f.setGoals_against(f.getGoals_against()+(goals_for2));

                    }
                    if(f.getName().equals(name1)){
                        f.setGoals_for(f.getGoals_for()+goals_for2);
                        f.setGoals_clean(f.getGoals_for()-f.getGoals_against());
                        if(goals_for2>goals_for1){
                            f.setPoints(f.getPoints()+3);
                        }
                        if(goals_for1==goals_for2){
                            f.setPoints(f.getPoints()+1);
                        }

                        f.setGoals_against(f.getGoals_against()+(goals_for1));

                    }
                }
                last_game--;
            }
            Collections.sort(footballs, new Comparator<Football>() {
                @Override
                public int compare(Football o1, Football o2) {
                    int x = o2.getPoints()-o1.getPoints();
                    int y =o2.getGoals_clean()-o1.getGoals_clean();
                    int z = o2.getGoals_for()-o1.getGoals_for();

                    if(x==0){
                        if(y==0){
                            if(z==0){
                                return o1.getName().compareTo(o2.getName());
                            }
                            return z;
                        }
                        return y;
                    }
                    return x;
                }
            });
            for (Football f:footballs){
                System.out.println(f.getName()+" "+f.getPoints()+" "+f.getGoals_for()+" "+f.getGoals_against());
            }
            System.out.println("END");
        }


    }
}




#暑期实习##网易互娱#
全部评论
我用的treemap,名字都没考虑
点赞 回复 分享
发布于 2022-04-17 17:41
用了个keyset 放在collection里面 重写了个比较器
点赞 回复 分享
发布于 2022-04-17 17:48
C++的话string默认重载了比较运算符,所以string的字典序排序直接用大于小于号就好了,Java的话不了解
点赞 回复 分享
发布于 2022-04-17 23:00
Java的话可以把所有信息放List里面,然后用Collections.sort(),用lamda表达式重新定义排序规则就好啦
点赞 回复 分享
发布于 2022-04-18 10:08
稍微吐槽一下,就一个临时的数据结构,没必要搞个什么getset吧🤨
点赞 回复 分享
发布于 2022-04-19 11:53

相关推荐

点赞 评论 收藏
分享
点赞 1 评论
分享
牛客网
牛客企业服务