自用PTA题目记录0021

博客记录了在PTA上解决7-22题——计算平均成绩的问题。给出了输入输出格式,以及一道样例,包括5位学生的学号、姓名和成绩。博主实现了计算平均成绩并输出低于平均成绩学生名单的功能,尽管对C++不熟悉,但还是成功完成了任务。

自用PTA题目记录0021

以下题目序号并无实际意义


7-22 计算平均成绩

题目作者: C课程组 单位: 浙江大学 代码长度限制: 16 KB 时间限制: 400 ms 内存限制: 64 MB

给定N个学生的基本信息,包括学号(由5个数字组成的字符串)、姓名(长度小于10的不包含空白字符的非空字符串)和成绩([0,100]区间内的整数),要求计算他们的平均成绩,并顺序输出平均线以下的学生名单。

输入格式: 输入在一行中给出正整数N(≤10)。随后N行,每行给出一位学生的信息,格式为“学号 姓名 成绩”,中间以空格分隔。

输出格式: 首先在一行中输出平均成绩,保留2位小数。然后按照输入顺序,每行输出一位平均线以下的学生的姓名和学号,间隔一个空格。

输入样例:
5
00001 zhang 70
00002 wang 80
00003 qian 90
10001 li 100
21987 chen 60

输出样例:
80.00
zhang 00001
chen 21987

代码

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

typedef struct Student
{
    string num;
    char name[20];
    float score;
} Student, *student;

typedef struct
{
    Student *student;
    int length;
} StudentList;

void sort(StudentList &slist);

int main(int argc, char const *argv[])
{
    StudentList Slist;
    float avgScore = 0;

    cin >> Slist.length;
    Slist.student = new Student[Slist.length];

    sort(Slist);

    for (int i = 0; i < Slist.length; i++)
    {
        cin >> Slist.student[i].num;
        cin >> Slist.student[i].name;
        cin >> Slist.student[i].score;
        avgScore += Slist.student[i].score;
    }
    avgScore /= Slist.length;

    printf("%.2f\n", avgScore);

    for (int i = 0; i < Slist.length; i++)
    {
        if (Slist.student[i].score < avgScore)
        {
            cout << Slist.student[i].name << " " << Slist.student[i].num << endl;
        }
    }

    return 0;
}
void sort(StudentList &list)
{
    int i, j, k;
    struct Student temp;
    for (i = 0; i < list.length - 1; i++)
    {
        k = i;
        for (j = i + 1; j < list.length; j++)
            if (list.student[j].score > list.student[k].score)
                k = j;
        temp = list.student[k];
        list.student[k] = list.student[i];
        list.student[i] = temp;
    }
}

总结

虽然C++苦手,不过这点程度还是勉强能写一写的
┓( ´∀` )┏

PTA提交通过截图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值