计算平均?平均

2023-09-11 07:23:24 作者::華麗旳謝幕

我有这个问题的问题:

鉴于试验结果的列表(每个测试日,学生证,和学生的得分);返回的最终得分为每个学生。学生的最终成绩的计算方法为他/她5最高测试成绩的平均值。你可以假设每个学生至少有5测试成绩。

Given a list of test results (each with a test date, Student ID, and the student’s Score); return the Final Score for each student. A student’s Final Score is calculated as the average of his/her 5 highest test scores. You can assume each student has at least 5 test scores.

现在,这里的东西。我不想要的答案。

Now, here is the thing. I do not want the answer.

我尝试了散列,但散列可以提供关键,地址,因此没有为我工作。

I tried the hashing, but hashing can provide key with addresses so it did not work for me.

我想用数组列表以为迭代计算的平均回报前五名成绩,但如何分配这些数字与studentID?

I thought of using array list the iterate thought to calculate the average and return the top five score, but how do I assign the numbers with the studentID ?

说我想要的输出为:迈克,15。这不是什么问题需要? 要获得学生ID和的平均数。 请帮我出一些端倪,我学习。

Say I want the output to be: Mike, 15. Is not this what the question requires ? To get the student ID and the average number. Please, help me out with some clues, I am learning.

推荐答案

仅仅通过学生证的测试结果(为主要排序键)和降分(小类键)进行排序,然后通过收集如下(伪code):

Just sort the test results by student ID (as the major sort key) and descending score (minor sort key) then go through the collection as follows (pseudo-code):

lastId = element[0].Id - 1
for each record in element[]:
    if record.Id != lastId:
        lastId = record.Id
        counter = 5
        sum = 0
    if counter > 0:
        sum = sum + record.score
        counter = counter - 1
        if counter == 0:
            print "Student ", record.Id, " got average of ", (sum / 5)

由于数据已排序,你知道,所有的成绩对于一个给定的学生在一起,而前五个都是最高的。因此,上述code将让您的工作这一切了。

Because the data is sorted, you know that all the scores for a given student are together and that the first five of them are the highest. Hence the above code will allow you to work it all out.

请记住,这取决于你对每个学生都至少有五个结果的规则。如果没有这些,你就必须改变平均计算code和可能做一些后期工作环上的最后一个学生。

Keep in mind this relies on your "every student has at least five results" rule. Without that, you'd have to change the average calculation code and possibly do some post-work loop on the last student.

 
精彩推荐