牛客网 沃老师学生的成绩(STL应用)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bengshakalakaka/article/details/79943392

题目链接:点击打开链接

应用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;
}



猜你喜欢

转载自blog.csdn.net/bengshakalakaka/article/details/79943392
今日推荐