Home Web Board ProblemSet Standing Status Statistics
 

Problem I: 成绩排序

Problem I: 成绩排序

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 490  Solved: 403
[Submit][Status][Web Board]

Description

定义Student类:

1. 数据成员string name和int score表示一个学生的姓名、成绩。

2. 无参构造函数。

3. void setStudent(string,int)方法,用于设置一个学生的属性值。

4. 重载>(大于运算符)。Student类的对象A和B的大小关系,A>B定义为A.score>B.score,或者A.score=B.score但A.name<B.name。

5.重载运算符<<用于输出学生信息:先输出成绩再输出姓名,中间用一个空格隔开。

Input

分多行。第一个M>0表示有M个学生信息。

之后有M行,每一行是一个学生信息。第一部分是学生姓名,第二部分是学生成绩。

Output

输出为M行,按照从大到小的顺序依次输出每个学生的成绩、姓名。假定不存在重名的学生。

Sample Input

5
Tom 98
Jack 97
Mary 98
Sherry 99
Dock 97

Sample Output

99 Sherry
98 Mary
98 Tom
97 Dock
97 Jack

HINT

string类有个方法:int compare(const string &s) const;用于比较当前字符串和s的大小,其原理等同于C语言的库函数strcmp。

Append Code

[Submit][Status][Web Board]

#include<iostream>
#include<cstring>
#include<string>
#define null ""
using namespace std;
class Student{
public:
string name;
int score;
Student(string n=null,int s=):name(n),score(s){}
void setStudent(string n,int s)
{
name=n;
score=s;
}
int operator>(Student &s)
{
if(score>s.score)
return ;
else if(score==s.score&&name<s.name)
return ;
else
return ;
} friend ostream &operator<<(ostream &os,Student &s);
};
ostream &operator<<(ostream &os,Student &s)
{
os<<s.score<<" "<<s.name;
return os;
}
int main()
{
int cases;
string name;
int score;
cin>>cases;
Student students[cases], temp;
for (int i = ; i < cases; i++)
{
cin>>name>>score;
students[i].setStudent(name, score);
}
for (int i = ; i < cases; i++)
{
for (int j = ; j < cases - i - ; j++)
{
if (!(students[j] > students[j + ]))
{
temp = students[j];
students[j] = students[j + ];
students[j + ] = temp;
}
}
}
for (int i = ; i < cases; i++)
cout<<students[i]<<endl;
return ;
}

最新文章

  1. 删除流氓软件McAfee
  2. Codeforces632E Thief in a Shop(NTT + 快速幂)
  3. Linux的学习之路
  4. 123——Appium Girls活动
  5. JavaScript hasOwnProperty() 函数详解
  6. 帮你选处理器:CPU T9500-p9500-T9400-T9300-p8700对比分析!
  7. [反汇编练习] 160个CrackMe之014
  8. SQL Server常见问题总结
  9. awk 多分隔符
  10. org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x0) was found in the CDATA sectio
  11. getBoundingClientRect获取元素在页面上的位置
  12. ABAP开发规范
  13. Class create, device create, device create file【转】
  14. BZOJ 3498: PA2009 Cakes 一类经典的三元环计数问题
  15. redis有序集合性能 列表、集合、有序集合
  16. redis 的 docker 镜像使用
  17. T-SQL查询的逻辑处理过程
  18. &quot;Unchecked-Send&quot;漏洞分析
  19. WordPress主题开发:数据调用
  20. php curl-class post

热门文章

  1. Android Matirx的简介
  2. SmartDoc(YUIDoc) 注释编写
  3. CentOS linux系统搭建LAMP环境
  4. 【算法题】Multiples of 3 and 5
  5. Nightwatch.js – 轻松实现浏览器的自动测试
  6. Android 常见工具类封装
  7. php + Redis 写的类似于新浪微博的feed系统
  8. Eclipse魔法堂:任务管理器
  9. 安装percona-xtrabackup一直提示依赖冲突的一个解决办法
  10. 使用VS2010开发Qt程序的一点经验