原题链接:蓝桥杯算法提高VIP-分数统计
解题思路:
注意事项:
参考代码:
import java.util.*;
public class TT1500 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
ArrayList<Integer> A = new ArrayList<Integer>();
ArrayList<Integer> B = new ArrayList<Integer>();
ArrayList<Integer> C = new ArrayList<Integer>();
ArrayList<Integer> D = new ArrayList<Integer>();
ArrayList<Integer> E = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
Map<String,Integer> map = new HashMap<String,Integer>();
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= 90) {
A.add(arr[i]);
if (map.containsKey("A")) {
map.put("A",map.get("A") + 1);
} else {
map.put("A",1);
}
} else if (arr[i] <= 89 && arr[i] >= 80) {
B.add(arr[i]);
if (map.containsKey("B")) {
map.put("B",map.get("B") + 1);
} else {
map.put("B",1);
}
} else if (arr[i] <= 79 && arr[i] >= 70) {
C.add(arr[i]);
if (map.containsKey("C")) {
map.put("C",map.get("C") + 1);
} else {
map.put("C",1);
}
} else if (arr[i] <= 69 && arr[i] >= 60) {
D.add(arr[i]);
if (map.containsKey("D")) {
map.put("D",map.get("D") + 1);
} else {
map.put("D",1);
}
} else {
E.add(arr[i]);
if (map.containsKey("E")) {
map.put("E",map.get("E") + 1);
} else {
map.put("E",1);
}
}
}
//给list集合从小到大排序
Collections.sort(A);
Collections.sort(B);
Collections.sort(C);
Collections.sort(D);
Collections.sort(E);
//通过set遍历map后找最大value
int max = 0;
Set<String> set = map.keySet();
for (String i : set) {
System.out.print(map.get(i) + " ");
if (max < map.get(i)) {
max = map.get(i);}
}
String key = "";
//通过value找key
for (Map.Entry entry : map.entrySet()) {
if (entry.getValue().equals(max)) {
key = entry.getKey() + "";
}
}
System.out.println();
System.out.println(max);
if (key.equals("A")) {
for (int i = A.size() - 1; i >= 0; i--) {
System.out.print(A.get(i) + " ");
}
} else if (key.equals("B")) {
for (int i = B.size() - 1; i >= 0; i--) {
System.out.print(B.get(i) + " ");
}
} else if (key.equals("C")) {
for (int i = C.size() - 1; i >= 0; i--) {
System.out.print(C.get(i) + " ");
}
} else if (key.equals("D")) {
for (int i = D.size() - 1; i >= 0; i--) {
System.out.print(D.get(i) + " ");
}
} else {
for (int i = E.size() - 1; i >= 0; i--) {
System.out.print(E.get(i) + " ");
}
}
}
}0.0分
1 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复