民生银行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"); } }