學生分數管理系統 (C++測試)


提交解答

分數: 100
時間限制: 5.0s
記憶體限制: 256M

作者:
題目代碼
題目類型
允許的語言
C++

C++ 題目:學生分數管理系統

題目描述

請設計一個程式,用來管理學生的姓名和分數,實現以下功能:

  1. 輸入 N 個學生的姓名與分數。
  2. 找出分數最高的學生,並輸出其姓名和分數。
  3. 計算並輸出所有學生的平均分數。
  4. 將學生按分數從高到低排序,並依序輸出姓名與分數。

輸入格式

  • 第一行為整數 N (1 ≤ N ≤ 100),表示學生人數。
  • 接下來 N 行,每行包含一個學生的姓名(不含空格,長度 ≤ 20)和一個整數分數(0 ≤ 分數 ≤ 100),以空格分隔。
範例輸入
3
Alice 85
Bob 92
Charlie 78

輸出格式

  1. 第一行輸出分數最高的學生,格式如下:
最高分學生: 姓名 分數

若有多位最高分學生,輸出第一個輸入的即可。

  1. 第二行輸出所有學生的平均分數,保留一位小數:
平均分數: 平均分數值
  1. 接下來輸出排序後的學生名單(分數從高到低),每行格式如下:
姓名 分數
範例輸出
最高分學生: Bob 92
平均分數: 85.0
排序後學生:
Bob 92
Alice 85
Charlie 78

題目要求

  1. 使用 struct 定義學生資料結構。
  2. 至少使用一個函式計算平均分數。
  3. 至少使用一個函式找出分數最高的學生。
  4. 可以使用 STL 容器(如 vector)或陣列。
  5. 分數排序可使用任何排序方法(內建 sort 或自行實作)。

參考實作(C++)

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>  // for std::setprecision

using namespace std;

// 定義學生結構
struct Student {
    string name;
    int score;
};

// 計算平均分數
double calculateAverage(const vector<Student>& students) {
    int sum = 0;
    for (const auto& s : students) {
        sum += s.score;
    }
    return static_cast<double>(sum) / students.size();
}

// 找出最高分學生
Student findTopStudent(const vector<Student>& students) {
    Student top = students[0];
    for (const auto& s : students) {
        if (s.score > top.score) {
            top = s;
        }
    }
    return top;
}

int main() {
    int N;
    cin >> N;
    vector<Student> students(N);

    // 輸入學生資料
    for (int i = 0; i < N; ++i) {
        cin >> students[i].name >> students[i].score;
    }

    // 找出最高分學生
    Student topStudent = findTopStudent(students);

    // 計算平均分數
    double avg = calculateAverage(students);

    // 排序(分數由高到低)
    sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
        return a.score > b.score;  // 高分在前
    });

    // 輸出結果
    cout << "最高分學生: " << topStudent.name << " " << topStudent.score << endl;
    cout << fixed << setprecision(1);
    cout << "平均分數: " << avg << endl;
    cout << "排序後學生:" << endl;
    for (const auto& s : students) {
        cout << s.name << " " << s.score << endl;
    }

    return 0;
}

舊版本參考實作(C++03)

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>  // for setprecision

using namespace std;

// 定義學生結構
struct Student {
    string name;
    int score;
};

// 計算平均分數(使用傳統 for loop)
double calculateAverage(const vector<Student>& students) {
    int sum = 0;
    for (size_t i = 0; i < students.size(); ++i) {
        sum += students[i].score;
    }
    return static_cast<double>(sum) / students.size();
}

// 找出最高分學生(使用傳統 for loop)
Student findTopStudent(const vector<Student>& students) {
    Student top = students[0];
    for (size_t i = 0; i < students.size(); ++i) {
        if (students[i].score > top.score) {
            top = students[i];
        }
    }
    return top;
}

// 比較函式,用於排序(由高到低)
bool compareScore(const Student& a, const Student& b) {
    return a.score > b.score;
}

int main() {
    int N;
    cin >> N;
    vector<Student> students(N);

    // 輸入學生資料
    for (int i = 0; i < N; ++i) {
        cin >> students[i].name >> students[i].score;
    }

    // 找出最高分學生
    Student topStudent = findTopStudent(students);

    // 計算平均分數
    double avg = calculateAverage(students);

    // 排序(分數由高到低)
    sort(students.begin(), students.end(), compareScore);

    // 輸出結果
    cout << "最高分學生: " << topStudent.name << " " << topStudent.score << endl;
    cout << fixed << setprecision(1);
    cout << "平均分數: " << avg << endl;
    cout << "排序後學生:" << endl;
    for (size_t i = 0; i < students.size(); ++i) {
        cout << students[i].name << " " << students[i].score << endl;
    }

    return 0;
}

評論

目前沒有評論。