29.结构体求出班级中最高分和学号,将学生信息按照成绩从高到低输出。

  1. 一个班不超过40个人,人员信息有成绩和学号组成,班级人数从键盘输入,完成函数,实现求出班级中最高分和学号,将学生信息按照成绩从高到低输出。
#include  <stdio.h>
#define ARR_SIZE 40
struct student
{
    int st;
    long num;
};
int findmax(struct student score[],int n,long *pmaxnum)
{
    int i,max;
    max=score[0].st;
    for(i=0;i<n;i++)
        if(max<score[i].st)
            max=score[i].st;
        *pmaxnum=i;
        return max;
}
void sortscore(struct student *score,int n)
{
    int i,j;
    struct student temp1;
    for(i=0;i<n-1;i++)
        for(j=i+1;j<n;j++)
            if (score[i].st<score[j].st)
            {
                temp1=score[i];score[i]=score[j];score[j]=temp1;                
            }   
}
int main()
{
    long maxnum;
    struct student score[ARR_SIZE];
    int maxscore,n,i;
    printf("Please enter total number:");
    scanf("%d", &n);    /*从键盘输入学生人数n*/
    printf("Please enter the number and score:\n");
    for(i=0; i<n; i++)  {
        scanf("%ld%d", &score[i].num,&score[i].st);
    }
    maxscore = findmax(score,n, &maxnum);  /*计算最高分及学生学号*/
    printf("maxscore = %d, maxnum = %ld\n", maxscore, maxnum);
    sortscore(score,n);
    printf("The sort after data is :\n");
    for(i=0;i<n;i++)
        printf("%ld,%d\n",score[i].num,score[i].st);
    return 0;
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值