分析:同学生成绩的道理一样,就是一个冒泡排序,这次是从小到大
代码如下:
#include <stdio.h>
#define N 5
struct Comp{
char name[50];
double score;
};
void DDubbleSort(struct Comp comp[]){
for(int i = 0; i < N-1 ; i++){
for(int j = 0; j < N-i-1 ;j++){
if(comp[j].score > comp[j+1].score){ //比较成绩的大小
struct Comp temp = comp[j+1]; //成绩大的交换所在的数组元素
comp[j+1] = comp[j];
comp[j] = temp;
}
}
}
}
int main(){
int n;
struct Comp comp[N]={{"zs",76},
{"ls",67},
{"ww",80},
{"zl",85},
{"xq",70}
};
DDubbleSort(comp);
printf("输入你要查询的名次(1-5):\n");
scanf("%d",&n);
printf("排名第%d的人是%s\n",n,comp[N-n].name);
}
运行效果: