蓝桥杯 算法训练 P1102(c语言版详细注释)

试题 算法训练 P1102

                                                                                  蓝桥杯试题解答汇总链接

资源限制

       时间限制:1.0s 内存限制:256.0MB


问题描述

       定义一个学生结构体类型student,包括4个字段,姓名、性别、年龄和成绩。然后在主函数中定义一个结构体数组(长度不超过1000),并输入每个元素的值,程序使用冒泡排序法将学生按照成绩从小到大的顺序排序,然后输出排序的结果。


输入格式

       第一行是一个整数N(N<1000),表示元素个数;接下来N行每行描述一个元素,姓名、性别都是长度不超过20的字符串,年龄和成绩都是整型。


输出格式

       按成绩从小到大输出所有元素,若多个学生成绩相同则成绩相同的同学之间保留原来的输入顺序。


样例输入

3
Alice female 18 98
Bob male 19 90
Miller male 17 92

样例输出

Bob male 19 90
Miller male 17 92
Alice female 18 98

代码

#include<stdio.h>
struct student{
	char name[21];//姓名 
	char sex[7];//性别 
	int age;//年龄 
	int score;//得分 
};
int main()
{
	int n,i,j;
	scanf("%d",&n);
	struct student stu[n],t;
	for(i=0;i<n;i++){
		scanf("%s%s%d%d",&stu[i].name,&stu[i].sex,&stu[i].age,&stu[i].score);
	}
	for(i=0;i<n;i++){//冒泡排序 
		for(j=n-1;j>i;j--){
			if(stu[j-1].score>stu[j].score){
				t=stu[j];
				stu[j]=stu[j-1];
				stu[j-1]=t;
			}
		}
		printf("%s %s %d %d\n",stu[i].name,stu[i].sex,stu[i].age,stu[i].score);
	}
	return 0;
}
发布了51 篇原创文章 · 获赞 58 · 访问量 4616

猜你喜欢

转载自blog.csdn.net/xyf0209/article/details/104533088