计蒜客_成绩排序

计蒜客_成绩排序

小蒜给出了班里某门课程的成绩单,请你按成绩从高到低对成绩单排序输出,如果有相同分数则名字字典序小的在前。

输入格式
第一行为 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;  
}  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章