学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun, 它的功能使:把分数最高的学生数据放在h所指的数组中,注意:分数最高的学生可能不止一个,函数返回分数最高的学生的人数

/*学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,
它的功能使:把分数最高的学生数据放在h所指的数组中,注意:分数最高的学生可能不止一个,函数返回分数最高的学生的人数。 */

#include <stdio.h>
#include <string.h>
#define MAX 100
typedef struct student
{
    int stu_id;
    int score;
}STU;
int fun(STU *s,STU *h,int n)
{
    int count=0;
    int max=0;
    for(int i=0;i<n;i++)
    {
        if(s[i].score >max)
        {
            max=s[i].score;
        }
    }
    for(int i=0;i<n;i++)
    {
        if(s[i].score == max)
        {
            max=s[i].score;
            h[count]=s[i];
            count++;
        }
    }
    return count;
}
int main()
{
    STU s[MAX];
    STU h[MAX];
    int n;
    printf("please enter studnet people\n");
    scanf("%d", &n);
    for(int i=0;i<n;i++)
    {
        printf("please enter %d student stu_id score\n",i+1);
        scanf("%d%d",&s[i].stu_id,&s[i].score);
    }
    n=fun(s,h,n);
    printf("count:%d\n",n);
    for(int i=0;i<n;i++)
    {
        printf("stu_id:%d score:%d\n",h[i].stu_id,h[i].score);
    }
    return 0;
}

posted on   wessf  阅读(95)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示