成绩排序

题目描述
有N个学生的数据,将学生数据按成绩从低到高排序,如果成绩相同则按姓名字符的字典序由小到大排序,如果姓名的字典序也相同则按照学生的年龄从小到大排序,并输出N个学生排序后的信息。
输入描述:
测试数据有多组,每组输入第一行有一个整数N(N<=1000),接下来的N行包括N个学生的数据。
每个学生的数据包括姓名(长度不超过100的字符串)、年龄(整形数)、成绩(小于等于100的正数)。
输出描述:
将学生信息按成绩进行排序,成绩相同的则按姓名的字母序进行排序。
然后输出学生信息,按照如下格式:
姓名 年龄 成绩

学生姓名的字母序区分字母的大小写,如A要比a的字母序靠前(因为A的ASC码比a的ASC码要小)。 示例1 输入
3
abc 20 99
bcd 19 97
bed 20 97
输出
bcd 19 97
bed 20 97
abc 20 99

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
struct student{
    char name[100];
    int age;
    int result;
}stu[1001];
int main(){
    int m;
    struct student temp;
    scanf("%d",&m);
    for(int i =0 ;i<m;i++){
        scanf("%s %d %d",&stu[i].name,&stu[i].age,&stu[i].result);
    }
    for(int i = 0;i<m;i++){
    	for(int j = i+1;j<m;j++){
    		if(stu[i].result>stu[j].result){
    			temp  = stu[j];
    			stu[j] = stu[i];
    			stu[i] = temp; 
			}
		}
	}
	for(int i = 0;i<m;i++){
		for(int j = i+1;j<m;j++){
			if(stu[i].result == stu[j].result){
				if(strcmp(stu[i].name,stu[j].name)>0)
				{
					temp  = stu[j];
	    			stu[j] = stu[i];
	    			stu[i] = temp; 
				}
				else if(strcmp(stu[i].name,stu[j].name)==0)
				{
					if(stu[i].age>stu[j].age){
						temp  = stu[j];
		    			stu[j] = stu[i];
		    			stu[i] = temp; 
					}
				}
			}
		}
	}
	for(int i = 0;i<m;i++)
	{
		printf("%s %d %d\n",stu[i].name,stu[i].age,stu[i].result);
	}
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值