您的位置:首页 > 其它

题目1061:成绩排序

2014-05-15 20:45 183 查看
题目描述:
有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码要小)。

代码很简单。。。

#include<iostream>

#include <stdio.h>

#include <algorithm>

#include <string>

#include <string.h>

using namespace std;

struct Student

{

string name;

int age,score;

Student(){name="";age=0;score=0;}

};

bool cmp(Student a,Student b)

{

if (a.score==b.score)

{

if (a.name==b.name)

return a.age<b.age;

else

return a.name<b.name;

}

else

return a.score<b.score;

}

int main()

{

int i=0,n;

while (scanf("%d",&n)!=EOF)

{

//每个学生的数据包括姓名(长度不超过100的字符串)、年龄(整形数)、成绩(小于等于100的正数)。

Student *stu=new Student
;

string *name=new string
;

int *age = new int
;

int *score = new int
;

for (i=0;i<n;i++)

{

cin>>stu[i].name>>stu[i].age>>stu[i].score;

}

sort(stu,stu+n,cmp);

for (i=0;i<n;i++)

{

cout<<stu[i].name.c_str()<<" "<<stu[i].age<<" "<<stu[i].score<<"\n";

}

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: