从键盘输入4 个学生的数据(包括姓名、年龄和成绩),按成绩从高到底排序,并输出其中成绩次高者的所有数据。
输入4行,每一行表示一个学生的数据,分别表示 姓名、年龄和成绩。
输出4行,表示排序后的结果,格式参见样例。
aa 10 90
bb 20 100
cc 18 98
dd 19 93
bb 20 100
cc 18 98
dd 19 93
aa 10 90
1 #include<stdio.h> 2 #include<math.h> 3 #include<string.h> 4 struct student 5 { 6 int num; 7 char name[105]; 8 int score; 9 }stu[5],t; 10 11 12 int main() 13 { 14 int i,j; 15 16 for(i=0;i<4;i++) 17 scanf("%s%d%d",stu[i].name,&stu[i].num,&stu[i].score); 18 19 for(i=0;i<4;i++) 20 { 21 for(j=0;j<4-i;j++) 22 { 23 if(stu[j].score<stu[j+1].score) 24 { 25 t=stu[j]; 26 stu[j]=stu[j+1]; 27 stu[j+1]=t; 28 } 29 } 30 } 31 for(i=0;i<4;i++) 32 printf("%s %d %d\n",stu[i].name,stu[i].num,stu[i].score); 33 return 0; 34 35 36 }
原文:https://www.cnblogs.com/chenyu-123/p/13939418.html