九度题目1061:成绩排序

机试练习第二题:九度 九度题目1061:成绩排序

题目1061:成绩排序

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:18914

解决:5298

题目描述:

    有N个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母序排序,如果姓名的字母序也相同则按照学生的年龄排序,并输出N个学生排序后的信息。

输入:

    测试数据有多组,每组输入第一行有一个整数N(N<=1000),接下来的N行包括N个学生的数据。
    每个学生的数据包括姓名(长度不超过100的字符串)、年龄(整形数)、成绩(小于等于100的正数)。

输出:

    将学生信息按成绩进行排序,成绩相同的则按姓名的字母序进行排序。
    然后输出学生信息,按照如下格式:
    姓名 年龄 成绩

样例输入:
3
abc 20 99
bcd 19 97
bed 20 97
样例输出:
bcd 19 97
bed 20 97
abc 20 99
提示:

学生姓名的字母序区分字母的大小写,如A要比a的字母序靠前(因为A的ASC码比a的ASC码要小)。

来源:
2000年清华大学计算机研究生机试真题

本题想法是仍采用sort函数,自己写一个比较函数com,但是写出来之后运行时错误 Runtime Error, 所以还是用一般方法一点点,排序

用sort函数,RE,,,目前还不知道为啥。。。(后续再改)
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std; 
	struct stu{
		string name;
		int age;
		int score;
		
	};
int cmp(struct stu stu1,struct stu stu2){
	if(stu1.score>stu2.score)
	return -1;
	else if(stu1.score<stu2.score)
	return 1;
	else if(stu1.score==stu2.score){
	
	if(stu1.name>stu2.name)
	return -1;
	else if(stu1.name<stu2.name)
	return 1;
	else if(stu1.name==stu2.name)
	if(stu1.age>stu2.age)
	return -1;
	else return -1;
	}
} 
int main(){
	

//	int cmp (struct stu1,struct stu2);
	
	
	struct stu student[1000];//定义一个结构体数组 
	int n;
  while(scanf("%d",&n)!=EOF){
	
	for(int i=0;i<n;i++){
		//scanf("%s%d%d",student[i].name,&student[i].age,&student[i].score);
		cin>>student[i].name>>student[i].age>>student[i].score;
	}
	sort(student,student+n,cmp);
	for(int i=0;i<n;i++){
		//printf("%s,%d,%d",student[i].name,student[i].age,student[i].score);
		cout<<student[i].name<<' '<<student[i].age<<' '<<student[i].score;
		printf("\n");
	}
  }
	return 0;
	
}


总结:运行时错误的原因:。。。

一般方法来排序,输出:
#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
struct student{  
    int age;  
    int score;  
    char name[102];  
};  
   
int main()  
{  
    struct student a[1002],temp;  
    int n,i,j;  
    while(scanf("%d",&n)!=EOF)  
    {  
        for(i=0;i<n;i++)  
            scanf("%s %d %d",a[i].name,&a[i].age,&a[i].score);  
        for(j=0;j<n;j++)  
        for(i=0;i<n-1-j;i++)  
        {  
            if(a[i].score>a[i+1].score)  
            {  
                temp=a[i];  
                a[i]=a[i+1];  
                a[i+1]=temp;  
            }  
            else if(a[i].score==a[i+1].score)  
            {  
                if(strcmp(a[i].name,a[i+1].name)>0)  
                {  
                    temp=a[i];  
                    a[i]=a[i+1];  
                    a[i+1]=temp;  
                }  
                else if(strcmp(a[i].name,a[i+1].name)==0)  
                {  
                    if(a[i].age>a[i+1].age)  
                    {  
                        temp=a[i];  
                        a[i]=a[i+1];  
                        a[i+1]=temp;  
                    }  
                }  
   
   
            }  
        }  
        for(i=0;i<n;i++)  
            printf("%s %d %d\n",a[i].name,a[i].age,a[i].score);  
   
    }  
    return 0;  
   
}  


终于ac了,,,

总结:sort太慢,,qsort和sort不能混用,乱用,结构体定义,细节问题。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值