Problem C: 成绩排序

Problem C: 成绩排序

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 2535   Solved: 1988
[ 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

5Tom 98Jack 97Mary 98Sherry 99Dock 97

Sample Output

99 Sherry98 Mary98 Tom97 Dock97 Jack

HINT

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


Append Code

[ Submit][ Status][ Web Board]
#include <bits/stdc++.h>
using namespace std;
class Student
{
private :
     string name;
     int score;
public :
     Student():name( "" ),score(0){}
     ~Student(){}
     void setStudent(string n, int s)
     {
         name = n;
         score = s;
     }
     bool operator>(Student &s)
     {
         if (score>s.score)
             return true ;
         else if (score==s.score)
         {
             if (name<s.name)
                 return true ;
             else
             return false ;
         }
         else
             return false ;
     }
     friend 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 = 0; i < cases; i++)
     {
        cin>>name>>score;
        students[i].setStudent(name, score);
     }
     for ( int i = 0; i < cases; i++)
     {
         for ( int j = 0; j < cases - i - 1; j++)
         {
             if (!(students[j] > students[j + 1]))
             {
                 temp = students[j];
                 students[j] = students[j + 1];
                 students[j + 1] = temp;
             }
         }
     }
     for ( int i = 0; i < cases; i++)
         cout<<students[i]<<endl;
     return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值