教你夺冠


私信TA

用户名:835685327

访问量:148526

签 名:

相互交流 相互学习

等  级
排  名 13
经  验 21593
参赛次数 0
文章发表 84
年  龄 0
在职情况 学生
学  校 辣鸡施工大学
专  业

  自我简介:

努力刷题 熟能生巧!

参考代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//定义一个学生信息结构体
typedef struct
{
    char *id;
    char *name;
    int  score1;
    int  score2;
    int  score3;
}Stu;

//输入一个学生信息
Stu *inputStu()
{
    Stu *stutmp;
    stutmp = (Stu*)malloc(sizeof(Stu));
    if (stutmp == NULL)
    {
        return NULL;
    }

    char idtmp[100];
    scanf("%s", idtmp);
    stutmp->id = (char*)malloc(strlen(idtmp) + 1);
    if (stutmp->id == NULL)
    {
        return NULL;
    }
    strcpy(stutmp->id, idtmp);

    char nametmp[100];
    scanf("%s", nametmp);
    stutmp->name = (char*)malloc(strlen(nametmp) + 1);
    if (stutmp->name == NULL)
    {
        return NULL;
    }
    strcpy(stutmp->name, nametmp);

    scanf("%d", &stutmp->score1);
    scanf("%d", &stutmp->score2);
    scanf("%d", &stutmp->score3);

    return stutmp;
}

//输出信息
void printStu(Stu *students[], int N)
{
    int totalscore1 = 0;
    int totalscore2 = 0;
    int totalscore3 = 0;
    int averscore1;
    int averscore2;
    int averscore3;

    int stutotalscores[N];

    int i;
    for (i = 0; i < N; i++)
    {
        totalscore1 += students[i]->score1;
        totalscore2 += students[i]->score2;
        totalscore3 += students[i]->score3;

        stutotalscores[i] = students[i]->score1 + students[i]->score2 + students[i]->score3;
    }

    averscore1 = totalscore1 / N;
    averscore2 = totalscore2 / N;
    averscore3 = totalscore3 / N;
    printf("%d %d %d\n", averscore1, averscore2, averscore3);
    
    //找到最高分对应的学生信息并打印
    int maxscores = 0;
    int maxindex = 0;
    for (i = 0; i < N; i++)
    {
        if (maxscores < stutotalscores[i])
        {
            maxscores = stutotalscores[i];
            maxindex = i;
        }
    }
    printf("%s %s %d %d %d\n", students[maxindex]->id, students[maxindex]->name, students[maxindex]->score1,
                                students[maxindex]->score2, students[maxindex]->score3);
}

void freeStu(Stu *stu)
{
    if (stu == NULL)
        return;

    free(stu->id);
    free(stu->name);
    free(stu);
}

int main()
{
    int N;
    scanf("%d", &N);

    Stu *students[N];
    int i;
    for (i = 0; i < N; i++)
    {
        students[i] = inputStu();
    }

    printStu(students, N);

    for (i = 0; i < N; i++)
    {
        free(students[i]);
    }

    return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区