C++系统性学习之考试成绩统计

C++系统性学习之考试成绩统计

在本篇博客中,我们将学习如何使用C++编写一个程序,根据给定的考试成绩,计算最高分、最低分和平均分。

问题描述

我们的任务是编写一个C++程序,接受用户输入的考试人数和每个学生的得分,然后计算并输出最高分、最低分和平均分。

解决方案

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>

using namespace std;

int main() {
    // 输入考试人数
    int n;
    cout << "请输入考试人数: ";
    cin >> n;

    vector<int> scores;

    // 循环读取每个学生的得分,并存储在向量中
    for (int i = 0; i < n; ++i) {
        int score;
        cout << "请输入第 " << i + 1 << " 个学生的得分: ";
        cin >> score;

        scores.push_back(score);
    }

    // 计算最高分、最低分和平均分
    int maxScore = *max_element(scores.begin(), scores.end());
    int minScore = *min_element(scores.begin(), scores.end());
    double avgScore = accumulate(scores.begin(), scores.end(), 0.0) / n;

    // 输出结果
    cout << maxScore << endl;
    cout << minScore << endl;
    cout << fixed << setprecision(2) << avgScore << endl;

    return 0;
}

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值