从键盘输入某班学生某门课的成绩和学号(最多不超过40人),当输入为负值时,表示输入结束,用函数编程通过返回数组中最大元素的下标,查找并输出成绩的最高分及其所在的学生学号

从键盘输入某班学生某门课的成绩和学号(最多不超过40人),当输入为负值时,表示输入结束,用函数编程通过返回数组中最大元素的下标,查找并输出成绩的最高分及其所在的学生学号。

**输入格式要求:"%ld%d"  提示信息:"Input student’s ID and score:"  "input error!\n"

**输出格式要求:"Total students are %d\n"  "The highest is:%ld, %d\n"

程序运行示例如下:

Input student’s ID and score:070310122 84

Input student’s ID and score:070310123 83

Input student’s ID and score:070310124 88

Input student’s ID and score:070310125 87

Input student’s ID and score:070310126 61

Input student’s ID and score:-1 -1

Total students are 5

The highest is:70310124, 88

#include <stdio.h>
#include <stdlib.h>
int main()
{
    long ID[41];
    int score[41];
    int j=0;
    do{
        printf("Input student’s ID and score:");
        scanf("%ld%d",&ID[j],&score[j]);
        j++;
    }while(ID[j-1]>0||score[j-1]>0);
    printf("Total students are %d\n",j-1);
    int t,i,k,a;
    for(i=0;i<j;i++)
    {
        for(k=1;k<j;k++)
        {
            if(score[k]>score[k-1])
            {
                t=score[k-1];
                score[k-1]=score[k];
                score[k]=t;
                a=ID[k-1];
                ID[k-1]=ID[k];
                ID[k]=a;
            }
        }
    }
    printf("The highest is:%ld, %d\n",ID[0],score[0]);
}

猜你喜欢

转载自blog.csdn.net/m0_73931587/article/details/127505190