思路:用结构体数组存放n个学生信息,采用选择法对各元素进行排序(比较各元素中的成绩)。
代码:
#include<stdio.h>
struct Student{
int num;
char name[20];
float score;
};
int main(){
struct Student stu[5]={
{1001,"zhangsan",80},
{1002,"lisisill",90},
{1003,"wangwuwu",88},
{1004,"liuliuli",98},
{1005,"wangwang",99}
};
struct Student temp;
const int n=5;
int i,j,k;
printf("the order id:\n");
for(i=0;i<n-1;i++){
k=i;
for(j=i+1;j<n;j++){
if(stu[i].score>stu[j].score){
k=j;
}
}
temp=stu[k];
stu[k]=stu[i];
stu[i]=temp;
}
for(i=0;i<n;i++){
printf("%6d %8s %6.2f\n",stu[i].num,stu[i].name,stu[i].score);
}
printf("\n");
return 0;
}
结果:
the order id:
1001 zhangsan 80.00
1003 wangwuwu 88.00
1002 lisisill 90.00
1004 liuliuli 98.00
1005 wangwang 99.00