【结构体】【sort】多重排序

本文介绍了一个使用C++编写的程序,该程序能够读取学生的姓名、总分、语文和数学成绩,并按照特定条件对学生信息进行排序。排序首先依据总分从高到低,总分相同时则按语文成绩从高到低,再相同则按数学成绩从高到低。通过这个程序,可以快速了解学生们的学术表现。

Just look at the code,that's OK;

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
struct student
{
	char name[20];
	int total;
	int chinese;
	int math;
}a[1005];
bool cmp(student x,student y)
{
	if(x.total>y.total)
		return 1;
	else if(x.total==y.total)
	{
		if(x.chinese>y.chinese)
			return 1;
		else if(x.chinese==y.chinese)
		{
			if(x.math>y.math)
				return 1;
		}
	}
	return 0;
}
int main()
{
	int n,i;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%s %d %d %d",a[i].name,&a[i].total,&a[i].chinese,&a[i].math);
	}
	sort(a,a+n,cmp);
	for(i=0;i<n;i++)
	{
		printf("%s\n",a[i].name);
	}
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值