10.有n个学生的信息(包括学号 姓名 成绩),要求按照成绩的高低顺序输出各学生的信息

思路:用结构体数组存放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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值