班级排名 算法提高 Java
代码:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class 班级排名
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext())
{
Map<String, Integer> map = new HashMap<String, Integer>();
int N = sc.nextInt();
for (int i = 0; i < N; i++)
map.put(sc.next(), 0);
int M = sc.nextInt();
for (int i = 0; i < M; i++)
{
int score = 0;
String name = "";
int scoreDaDa = 0;
for (int j = 0; j < N; j++)
{
score = sc.nextInt();
name = sc.next();
score += map.get(name);
map.put(name, score);
if ("DaDa".equals(name))
{
scoreDaDa = score;
}
}
int count = 1;
for (String key : map.keySet())
{
if (map.get(key) > scoreDaDa)
count++;
}
System.out.println(count);
}
}
sc.close();
}
}
本文深入探讨了使用Java实现的班级排名算法,通过具体代码示例展示了如何利用HashMap存储学生分数,并计算特定学生(如DaDa)在班级中的排名。算法涉及读取输入、更新学生分数、比较和确定排名等关键步骤。

被折叠的 条评论
为什么被折叠?



