如何从学生成绩和指数中找到中位数并将其设置为排名

问题描述 投票:0回答:1

您好,我有这组学生成绩,按降序排列,并带有各自的索引/序列号(根据班级规模自动生成)。

我想在学生队伍中实现这个目标

 Index(Serial No)          scores.            Rank
  1.                           95.0               2.0
    
  2.                           95.0               2.0
    
  3.                           95.0               2.0
    
  4.                           90.0               4.5
    
  5.                           90.0               4.5
    
  6.                           85.0               6.0
    
  7.                           70.0               9.0
    
  8.                           70.0               9.0
    
  9.                           70.0               9.0
    
  10.                          70.0               9.0
    
  11.                          70.0               9.0
    
  12.                          65.0              12.5
    
  13.                          65.0              12.5
    
  14.                          65.0              12.5
    
  15.                          65.0              12.5
    
  16.                          60.0              16.0
    
  17.                          40.0              17.0
    

对于索引号为1的学生1、2 和 3 他们排名第二,这意味着班级中没有第一。排名(在我们的例子中是 2)是通过找到这三个数字的中间值(中位数)来找到的。

索引6仍然在班级中排名第6,这还可以。 而且班级里没有排名5也没关系。

索引 4 和 5 的总计数为 2,为偶数,并且得分相等,因此我们找到中位数索引 (4+5)÷2=4.5

索引 7、8、9、10 和 11 的总计数为 5(奇数),因此得分相等,因此通过找到中位数 9 来确定排名。 (7+8+9+10+11)/5=9

所以我想要实现的是计算中位数,并对分数相同且计数为偶数的学生给予相同的排名。例如,下面的代码结果应该是 8.5 而不是 8.0,因为对于这些索引,中位数是 (6.0+7.0+8.0+9.0+10.0+11.0)/6=8.5,对于索引 12.0 和 13.0,中位数应该是 12.5,而不是12.0 自 (12.0+13.0)/2= 12.5 起。 注意:对于索引 1.0 到 5.0 的情况,效果很好。

这是我的代码,给出以下结果:-

 public class ViewStudentScores extends AppCompatActivity {

private DatabaseReference databaseReference;
private StudentAdapter studentAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_student_scores);

    RecyclerView recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    studentAdapter = new StudentAdapter();
    recyclerView.setAdapter(studentAdapter);
    databaseReference = FirebaseDatabase.getInstance().getReference("students");
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            List<Student> students = new ArrayList<>();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                Student student = snapshot.getValue(Student.class);
                students.add(student);
            }

            // Sort students by score in descending order
            Collections.sort(students, new Comparator<Student>() {
                @Override
                public int compare(Student student1, Student student2) {
                    return Double.compare(student2.getScore(), student1.getScore());
                }
            });

            // Assign ranks considering median of index numbers for students with the same score
            int totalStudents = students.size();
            int startIndex = 0;
            while (startIndex < totalStudents) {
                int endIndex = startIndex + 1;
                while (endIndex < totalStudents && students.get(startIndex).getScore() == students.get(endIndex).getScore()) {
                    endIndex++;
                }
                double medianIndex = (startIndex + endIndex - 1) / 2;
                double rank = medianIndex + 1;

                // Assign rank to students with the same score
                for (int i = startIndex; i < endIndex; i++) {
                    students.get(i).setRank(rank);
                }

                startIndex = endIndex;
            }

            // Update RecyclerView with the sorted and ranked students
            studentAdapter.setStudents(students);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            // Handle database error
        }
    });
}

}

java android firebase firebase-realtime-database
1个回答
0
投票

在计算中位数的那一行

double medianIndex = (startIndex + endIndex - 1) / 2;

您正在进行整数除法,因为两边都是整数,因为

startIndex
endIndex
是整数。要么将其中之一或两者都加倍,要么使除数
2
成为加倍。最简单的修复方法可能是:

double medianIndex = (startIndex + endIndex - 1) / 2.0;
© www.soinside.com 2019 - 2024. All rights reserved.