解题思路:使用list来存放个等级的成绩,通过collections的sort方法和reverse方法进行降序
注意事项:
参考代码:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); //获取n个数据 int arr[] = new int[n]; //构造成绩数组 for (int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); } ArrayList<Integer> gradeA = new ArrayList<>(); ArrayList<Integer> gradeB = new ArrayList<>(); ArrayList<Integer> gradeC = new ArrayList<>(); ArrayList<Integer> gradeD = new ArrayList<>(); ArrayList<Integer> gradeE = new ArrayList<>(); List[] o ={gradeA,gradeB,gradeC,gradeD,gradeE}; int countA = 0; int countB = 0; int countC = 0; int countD = 0; int countE = 0; for (int i : arr) { if (i >= 90) { countA++; gradeA.add(i); } else if (i >= 80) { countB++; gradeB.add(i); } else if (i >= 70) { countC++; gradeC.add(i); } else if (i >= 60) { countD++; gradeD.add(i); } else { countE++; gradeE.add(i); } } int[] grade = {countA, countB, countC, countD, countE}; int max = 0; for (int j = 0; j < grade.length; j++) { if (max < grade[j]) { max = grade[j]; } } for (int i : grade) { System.out.print(i+" "); } System.out.println(); System.out.println(max); for (List o1 : o) { if (o1.size()==max){ Collections.sort(o1); Collections.reverse(o1); for (Object o2 : o1) { System.out.print(o2+" "); } } } }
0.0分
1 人评分