1 条题解

  • 1
    @ 2024-11-3 17:42:15

    题目大意:输入n行薯片代表的字符串以及其对应的数量,按照题目要求进行排序。

    考察:结构体排序

    思路:题目所给的排序要求是,按照所给的字符串的英文大写首字母的字符序先升序排序,再按照数量进行降序排序。(注意:SC是小琳最喜欢的薯片,所以字符串为“SC”的只用排其对应数量,特判一下,输出的时候在最前面)。

    以下是两种代码的写法:

    string版本:

    #include<bits/stdc++.h>
    #define int long long
    #define endl '\n'
    using namespace std;
    const int N = 2e5 + 9;
    struct ymgg{
        string s;
        int c;
    }p[N];
    int ai[N];
    ​
    bool cmp(ymgg a, ymgg b) {
        if(a.s == b.s) return a.c > b.c;
        return a.s < b.s;
    }
    ​
    signed main() {
        ios::sync_with_stdio(false);
        cin.tie(0), cout.tie(0);
        
        int n, k = 0;
        cin >> n;
        for(int i = 1; i <= n; i ++) {
            cin >> p[i].s >> p[i].c;
            if(p[i].s == "SC") {
                k ++;
                ai[k] = p[i].c;
            }
        }
        sort(p + 1, p + 1 + n, cmp);
        sort(ai + 1, ai + 1 + k);
        for(int i = k; i >= 1; i --) cout << "SC " << ai[i] << endl; 
        for(int i = 1; i <= n; i ++) {
            if(p[i].s == "SC") continue;
            else cout << p[i].s << " " << p[i].c << endl;
        }
        return 0;
    }
    

    char版本:

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    #define endl "\n"
    #define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    const int N=2e5+5;
    const int inf=1e18;
    const int mod=1e9+7;
    struct op{
        char s[25];
        int cnt;
    }a[N];
    map<int,int>mp;
    bool cmp(op x,op y)
    {
        if(strcmp(x.s,y.s)==0)return x.cnt>y.cnt;
        if(strcmp(x.s,y.s)<0)return 1;
        else return 0;
    }
    void solve()
    {
        int n;
        cin >> n;
        for(int i=1;i<=n;i++)
        {
            cin >> a[i].s >> a[i].cnt;
            // cout << a[i].s <<endl;
        }
        sort(a+1,a+n+1,cmp);
    ​
        for(int i=1;i<=n;i++)
        {
            if(strcmp(a[i].s,"SC")==0)
            {
                cout << a[i].s  <<" "<< a[i].cnt <<endl;
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(strcmp(a[i].s,"SC")!=0)
            {
                cout << a[i].s <<" "<< a[i].cnt <<endl;
            }
        }
    }
    ​
    signed main()
    {
        IOS;
        // int t;
        // cin >> t;
        // while(t--)
        // {
            solve();
        // }
        return 0;
    }
    ​
    
    • 1

    琳琳爆零的那些年––ta爱的乐事薯片

    信息

    ID
    1045
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    递交数
    172
    已通过
    17
    上传者