1. 程式人生 > >牛客網 沃老師學生的成績(STL應用)

牛客網 沃老師學生的成績(STL應用)

題目連結:點選開啟連結

應用sort排序,注意去0操作,否則按照字串字典序帶0的會比不帶0的數值大,本來想用pair,結果沒有考慮去0所以涼涼

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
using namespace std;
const int MAX_N=200005;
#define inf 1<<23
typedef long long ll;
typedef long long LL;
struct stu
{
    string name;
    string score;
    string hh;
}p[MAX_N];
bool cmp(stu u,stu v)
{
    if(u.hh==v.hh)
    {
        return u.name<v.name;
    }
    return u.hh>v.hh;
}
int main()
{
    int n;
    int k;
    while(scanf("%d",&n)!=EOF)
    {

        for(int i=0; i<n; i++)
        {
            cin>>p[i].name;
            cin>>p[i].score;
            for(int j=p[i].score.size()-1;j>=0;j--)
            {
                if(p[i].score[j]!='0')
                {
                    k=j;
                    break;
                }
            }
            p[i].hh=p[i].score.substr(0,k+1);//取字串
        }
        stable_sort(p,p+n,cmp);
        for(int i=0; i<n; i++)
        {
            cout<<p[i].name<<" "<<p[i].score<<endl;
        }
    }
    return 0;
}