结构体例题(四)

来源:互联网 发布:js new date 少一天 编辑:程序博客网 时间:2024/05/16 11:15

【题目】有n个学生的信息(包括学号、姓名、成绩),要求按照成绩的高低顺序输出各个学生的信息。

#include<stdio.h>#define N 5int main(){struct student {long num;char name[20];int  score;}temp;    struct student stu[N]={{10101,"zhang",78},{10103,"wang",98},{10106,"li",86},{10108,"ling",73},{10110,"sun",100}};for(int i=0;i<N-1;i++){int k=i;for(int j=i+1;j<N;j++)if(stu[j].score>stu[k].score)k=j;temp=stu[i];stu[i]=stu[k];stu[k]=temp;}printf("The order is:\n");for( i=0;i<N;i++)printf("%ld %s %d\n",stu[i].num,stu[i].name,stu[i].score);printf("\n");return 0;}


原创粉丝点击