計蒜客_成績排序
小蒜給出了班裏某門課程的成績單,請你按成績從高到低對成績單排序輸出,如果有相同分數則名字字典序小的在前。
輸入格式
第一行爲 n(0 < n < 20),表示班裏的學生數目;
接下來的 n行,每行爲每個學生的名字和他的成績, 中間用單個空格隔開。名字只包含字母且長度不超過 20,成績爲一個不大於 100 的非負整數。
輸出格式
把成績單按分數從高到低的順序進行排序並輸出,每行包含名字和分數兩項,之間有一個空格。
樣例輸入
4
Kitty 80
Hanmeimei 90
Joey 92
Tim 28
樣例輸出
Joey 92
Hanmeimei 90
Kitty 80
Tim 28
//main.cpp
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef struct tagStudentinfo
{
int Score;
string strName;
bool operator < (tagStudentinfo const& _A) const
{ //這個函數指定排序策略,按成績排序,如果成績相等的話,按strName排序
if(Score > _A.Score) return true;
if(Score == _A.Score)
return strName.compare(_A.strName) < 0;
return false;
}
}Studentinfo; //學生信息
int main()
{
int nSize; //用學生分數映射學生姓名
map<Studentinfo, int>mapStudent;
map<Studentinfo, int>::iterator iter;
Studentinfo studentinfo;
int n=0;
cin>>n;
for(int i=0;i<n;i++){
string str;
int score;
cin>>str>>score;
studentinfo.Score = score;
studentinfo.strName = str;
mapStudent.insert(pair<Studentinfo, int>(studentinfo, 0));
}
for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)
cout<<iter->first.strName<<' '<<iter->first.Score<<endl;
return 0;
}