📌  相关文章
📜  三科平均分最高的学生

📅  最后修改于: 2022-05-13 01:57:08.825000             🧑  作者: Mango

三科平均分最高的学生

给定一个包含学生姓名和他/她在 3 个科目中得分的数据的文件。任务是找到平均分最高的学生列表。
注意:如果超过一名学生的平均分最高,请按照文件中的顺序打印。
例子:

方法 :

  1. 遍历文件数据并存储每个学生的平均分数。
  2. 现在,找到最高平均分并搜索所有具有该最高平均分的学生。
  3. 按照文件中的顺序打印最高平均分和姓名。

下面是上述方法的实现:

C++
// C++ program to find the
// list of students having maximum average score
#include
using namespace std;
 
// Function to find the
// list of students having maximum average score
// Driver code
void getStudentsList(string file[],int n)
{
    // Variables to store average score of a student
    // and maximum average score
    int avgScore;
    int maxAvgScore = INT_MIN;
 
    // List to store names of students
    // having maximum average score
    vector names;
 
    // Traversing the file data
    for (int i = 0; i < n; i += 4) {
 
        // finding average score of a student
        avgScore = (stoi(file[i + 1]) +
                    stoi(file[i + 2]) +
                stoi(file[i + 3])) / 3;
 
        if (avgScore > maxAvgScore) {
            maxAvgScore = avgScore;
 
            // Clear the list and add name of student
            // having current maximum average score in the list
            names.clear();
            names.push_back(file[i]);
        }
 
        else if (avgScore == maxAvgScore)
            names.push_back(file[i]);
    }
 
    // Printing the maximum average score and names
    // of students having this maximum average score
    // as per the order in the file.
    for (int i = 0; i < names.size(); i++) {
        cout <


Java
// Java program to find the
// list of students having maximum average score
 
import java.io.*;
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // Function to find the
    // list of students having maximum average score
    // Driver code
    static void getStudentsList(String[] file)
    {
        // Variables to store average score of a student
        // and maximum average score
        int avgScore;
        int maxAvgScore = Integer.MIN_VALUE;
 
        // List to store names of students
        // having maximum average score
        ArrayList names = new ArrayList<>();
 
        // Traversing the file data
        for (int i = 0; i < file.length; i += 4) {
 
            // finding average score of a student
            avgScore = (Integer.parseInt(file[i + 1]) +
                        Integer.parseInt(file[i + 2]) +
                       Integer.parseInt(file[i + 3])) / 3;
 
            if (avgScore > maxAvgScore) {
                maxAvgScore = avgScore;
 
                // Clear the list and add name of student
                // having current maximum average score in the list
                names.clear();
                names.add(file[i]);
            }
 
            else if (avgScore == maxAvgScore)
                names.add(file[i]);
        }
 
        // Printing the maximum average score and names
        // of students having this maximum average score
        // as per the order in the file.
        for (int i = 0; i < names.size(); i++) {
            System.out.print(names.get(i) + " ");
        }
 
        System.out.print(maxAvgScore);
    }
 
    // Driver code
    public static void main(String args[])
    {
        String[] file = { "Shrikanth", "20", "30",
                        "10", "Ram", "100", "50", "10" };
        getStudentsList(file);
    }
}


Python3
# Python3 program to find the list of
# students having maximum average score
 
# Function to find the list of students
# having maximum average score
def getStudentsList(file):
     
    # Variables to store maximum
    # average score
    maxAvgScore = 0
 
    # List to store names of students
    # having maximum average score
    names = []
 
    # Traversing the file data
    for i in range(0, len(file), 4):
 
        # finding average score
        # of a student
        avgScore = (int(file[i + 1]) +
                    int(file[i + 2]) +
                    int(file[i + 3])) // 3
 
        if avgScore > maxAvgScore:
            maxAvgScore = avgScore
 
            # Clear the list and add name
            # of student having current
            # maximum average score in the list
            names.clear()
            names.append(file[i])
             
        elif avgScore == maxAvgScore:
            names.add(file[i])
         
    # Printing the maximum average score and names
    # of students having this maximum average score
    # as per the order in the file.
    for i in range(len(names)):
        print(names[i], end = " ")
         
    print(maxAvgScore)
 
# Driver Code
if __name__ == "__main__":
     
    file = ["Shrikanth", "20", "30", "10",
            "Ram", "100", "50", "10"]
                     
    getStudentsList(file)
 
# This code is contributed
# by rituraj_jain


C#
// C# program to find the
// list of students having maximum average score
 
using System;
using System.Collections.Generic;
class GFG {
 
    // Function to find the
    // list of students having maximum average score
    // Driver code
    static void getStudentsList(string [] file)
    {
        // Variables to store average score of a student
        // and maximum average score
        int avgScore;
        int maxAvgScore = Int32.MinValue;
 
        // List to store names of students
        // having maximum average score
        List names = new List();
 
        // Traversing the file data
        for (int i = 0; i < file.Length; i += 4) {
 
            // finding average score of a student
            avgScore = (Int32.Parse(file[i + 1]) +
                        Int32.Parse(file[i + 2]) +
                    Int32.Parse(file[i + 3])) / 3;
 
            if (avgScore > maxAvgScore) {
                maxAvgScore = avgScore;
 
                // Clear the list and add name of student
                // having current maximum average score in the list
                names.Clear();
                names.Add(file[i]);
            }
 
            else if (avgScore == maxAvgScore)
                names.Add(file[i]);
        }
 
        // Printing the maximum average score and names
        // of students having this maximum average score
        // as per the order in the file.
        for (int i = 0; i < names.Count; i++) {
            Console.Write(names[i] + " ");
        }
 
        Console.WriteLine(maxAvgScore);
    }
 
    // Driver code
    public static void Main()
    {
        string[] file = { "Shrikanth", "20", "30",
                        "10", "Ram", "100", "50", "10" };
        getStudentsList(file);
    }
}
 
 
// This code is contributed by ihritik


PHP
 $maxAvgScore)
        {
            $maxAvgScore = $avgScore;
 
            // Clear the list and add name of
            // student having current maximum
            // average score in the list
            unset($names);
            $names = array();
            array_push($names, $file[$i]);
        }
 
        else if ($avgScore == $maxAvgScore)
            array_push($names, $file[$i]);
    }
 
    // Printing the maximum average score
    // and names of students having this
    // maximum average score as per the
    // order in the file.
    for ($i = 0; $i < count($names); $i++)
    {
        echo $names[$i] . " ";
    }
 
    echo $maxAvgScore;
}
 
// Driver code
$file = array( "Shrikanth", "20", "30", "10",
               "Ram", "100", "50", "10" );
 
// Number of elements in string array        
$n = count($file);
 
getStudentsList($file, $n);
 
// This code is contributed by mits
?>


Javascript


输出:
Ram 53