文章

145

粉丝

143

获赞

21

访问

38.0k

头像
成绩排序2.0 题解:C
P1159 清华大学上机题
发布于2024年1月31日 22:15
阅读数 380

#include <stdio.h>

typedef struct{
	int x;
	int s;
}Student;

//排序:先按成绩s从小到大;如果学生的成绩相同,则按照学号的大小进行从小到大排序。
void Sort(Student stu[],int n)
{
	//先按学号从小到大排序(学号互异)
	//再进行稳定排序(从最后向前每次选择最大的数放最后)
	int i,j;
	Student temp;
	for(i = 0; i < n-1; i++)	//学号排序
		for(j = 1; j < n-i; j++)
			if(stu[j].x < stu[j-1].x)
			{
				temp = stu[j];
				stu[j] = stu[j-1];
				stu[j-1] = temp;
			}
	for(i = 0; i < n-1; i++)
		for(j = 1; j < n-i; j++)
			if(stu[j].s < stu[j-1].s)
			{
				temp = stu[j];
				stu[j] = stu[j-1];
				stu[j-1] = temp;
			}
}

int main()
{
	int n,i;
	Student stu[100];
	scanf("%d",&n);
	for(i = 0; i < n; i++)
		scanf("%d %d",&stu[i].x,&stu[i].s);
	Sort(stu,n);
	for(i = 0 ;i < n; i++)
		printf("%d %d\n",stu[i].x,stu[i].s);
	
	
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发