解题思路:
注意事项:
参考代码:
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语言训练-求素数问题 (C语言代码)浏览:989 |
C语言程序设计教程(第三版)课后习题7.3 (C语言代码)浏览:1273 |
数列排序 (C语言代码)浏览:858 |
妹子杀手的故事 (C语言代码)浏览:1297 |
WU-字符串比较 (C++代码)浏览:824 |
2006年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:503 |
【明明的随机数】 (C语言代码)浏览:845 |
A+B for Input-Output Practice (III) (C语言代码)浏览:594 |
C语言程序设计教程(第三版)课后习题11.1 (C语言代码)浏览:525 |
永远的丰碑 (C语言代码)浏览:608 |