学生的记录由学号和成绩组成,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 2024-06-24 23:20  wessf  阅读(122)  评论(0)    收藏  举报