首页 > 民生银行2022届总行“民芯计划”校园招聘笔试编程题答案
头像
OceanLeee
编辑于 2021-06-27 11:00
+ 关注

民生银行2022届总行“民芯计划”校园招聘笔试编程题答案

两道题都AC了,大佬们提点意见,看看怎么能更好地优化啊?
第一题,成绩单排序
public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        ArrayList<String>[] gradeList = new ArrayList[n];
        for(int i = 0;i < n;i++){
            gradeList[i] = new ArrayList<>();
            gradeList[i].add(in.next());
            int chinGrade = in.nextInt();
            int mathGrade = in.nextInt();
            int engGrade = in.nextInt();
            int sum = chinGrade + mathGrade + engGrade;
            gradeList[i].add(String.valueOf(sum));
            gradeList[i].add(String.valueOf(chinGrade));
            gradeList[i].add(String.valueOf(mathGrade));
            gradeList[i].add(String.valueOf(engGrade));
        }

        Arrays.sort(gradeList, new Comparator<ArrayList<String>>() { @Override public int compare(ArrayList<String> o1, ArrayList<String> o2) {
                if(Integer.parseInt(o2.get(1)) != Integer.parseInt(o1.get(1))){
                    return Integer.parseInt(o2.get(1)) - Integer.parseInt(o1.get(1));
                }else if(Integer.parseInt(o2.get(2)) != Integer.parseInt(o1.get(2))){
                    return Integer.parseInt(o2.get(2)) - Integer.parseInt(o1.get(2));
                }else if(Integer.parseInt(o2.get(3)) != Integer.parseInt(o1.get(3))){
                    return Integer.parseInt(o2.get(3)) - Integer.parseInt(o1.get(3));
                }else if(Integer.parseInt(o2.get(4)) != Integer.parseInt(o1.get(4))){
                    return Integer.parseInt(o2.get(4)) - Integer.parseInt(o1.get(4));
                }else{
                    int i = 0;
                    while(i < Math.min(o1.get(0).length(),o2.get(0).length())){
                        if(o1.get(0).charAt(i) == o2.get(0).charAt(i)){ i++; }
                        else{ break; }
                    }
                    return Character.compare(o1.get(0).charAt(i),o2.get(0).charAt(i));
                }

            }
        });

        int chinGrade = 0,mathGrade = 0,engGrade =0,sum=0;
        StringBuilder sb = new StringBuilder();
        int rank = 0;
        for(int i = 0; i < n;i++){
            if(sum != Integer.parseInt(gradeList[i].get(1)) ||
            chinGrade != Integer.parseInt(gradeList[i].get(2))||
            mathGrade != Integer.parseInt(gradeList[i].get(3))||
            engGrade != Integer.parseInt(gradeList[i].get(4))){
                rank = i + 1;
            }

            sb.append("rank:" + rank + " name:")
                    .append(gradeList[i].get(0)+" total:")
                    .append(gradeList[i].get(1)+ " chinese:")
                    .append(gradeList[i].get(2)+ " math:")
                    .append(gradeList[i].get(3)+ " english:")
                    .append(gradeList[i].get(4));
            System.out.println(sb);
            sb.delete(0,sb.length());
            sum = Integer.parseInt(gradeList[i].get(1));
            chinGrade = Integer.parseInt(gradeList[i].get(2));
            mathGrade = Integer.parseInt(gradeList[i].get(3));
            engGrade = Integer.parseInt(gradeList[i].get(4));
        }
    }
第二题,测试菜单程序
public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String s = in.next();
        if(s.charAt(0) == 'Q'){
            System.out.println("Quit");
        }
        else if(s.charAt(0) == '1'){
            int count = 0;
            while(in.hasNextLine()){
                s = in.nextLine();
                if(s.trim().length() != 0){ count++; }
            }
            System.out.println(count);
        }
        else{
            System.out.println("Wrong input, Please re-choose");
            System.out.println("Menu Function Test");
            System.out.println("1: Count Lines");
            System.out.println("Q: Quit");
        }
    }



全部评论

(9) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐