学生成绩排名系统(c++实现)

  • 题目:学生有两门课语文和数学,实现一个班级名次排名,要求总分高的排在前面,当总分相同时,数学成绩高的在前面,若两门课程均相等时,按姓名拼音排序。
  • 输入(第一行输入 几个学生,第二行输入学生姓名,语文成绩,数学成绩)
3   
abc 88 89
aaa 88 89
bcd 87 90
  • 输出
bcd 87 90
aaa 88 89
abc 88 89
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

typedef struct{
	char name[100];
	int chinese;
	int math;
}student;

student stu[1001];

int compare(const void* a, const void* b){
	student* pa = (student*) a;
	student* pb = (student*) b;
	int num1 = pa->chinese + pa->math;
	int num2 = pb->chinese + pb->math;
	return num2 - num1;
}

int main(){
	int n;
	scanf("%d", &n);
	student temp;
	for(int i = 0; i < n; i++){
		scanf("%s %d %d", &stu[i].name, &stu[i].chinese, &stu[i].math);
	}

	qsort(stu, n, sizeof(student), compare);

	for(int i = 0; i < n; i++){
		for(int j = i + 1; j < n; j++){
			if((stu[i].chinese + stu[i].math) == (stu[j].chinese + stu[j].math)){
				if(stu[i].math < stu[j].math){
					temp = stu[j];
					stu[j] = stu[i];
					stu[i] = temp;
				}else if((stu[i].chinese == stu[j].chinese) && (stu[i].math == stu[j].math)){
               		if(strcmp(stu[i].name, stu[j].name) > 0){
                   		temp = stu[j];
                    	stu[j] = stu[i];
                    	stu[i] = temp;
               		}
				}
			}
		}	
	}

	for(int i = 0; i < n; i++){
	   printf("%s\t%d\t%d\n", stu[i].name, stu[i].chinese, stu[i].math);
	}
	return 0;
}
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值