第二题 成绩排序 (九度 2) P11

来源:互联网 发布:淘宝开店推广 编辑:程序博客网 时间:2024/06/03 13:57

与上一题一样没什么好说的

但是一定要注意重载的操作符的写法


struct node{

...

operator <(const node &b){

if....return .....

}

...

}


#include <iostream>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <string.h>




using namespace std;




struct node{char na[101];int ag;int g;
operator < (const node &b) const {
    if(g!=b.g)return g<b.g;
    int tmp=strcmp(na,b.na);
    if(!tmp)return tmp<0;
    else
        return ag<b.ag;
}




}no[1001];//这样用比较多




int main()
{
    int n;


    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
        cin>>no[i].na>>no[i].ag>>no[i].g;
        }




        sort(no,no+n);
        for(int i=0;i<n;i++)
        {
            cout<<no[i].na<<" "<<no[i].ag<<" "<<no[i].g<<endl;
        }








    }
    return 0;
}