1062 Talent and Virtue (25分)——sort排序

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct person
{
    string id;
    int virtue, talent, sum;
    int type;//12345
};
bool cmp(person a, person b)//按类别,总分,德分,学号排序
{
    if (a.type != b.type)
        return a.type < b.type;
    else if (a.sum != b.sum)
        return a.sum > b.sum;
    else if (a.virtue != b.virtue)
        return a.virtue > b.virtue;
    else
        return a.id < b.id;
}
int main()
{
    ios::sync_with_stdio(false);
    int N, L, H;
    cin >> N >> L >> H;
    struct person* p = new struct person[N];
    string id;
    int virtue, talent;
    int cnt = 0;
    for (int i = 0; i < N; ++i)
    {
        cin >> id >> virtue >> talent;
        if (virtue >= L && talent >= L)
        {
            p[cnt].id = id;
            p[cnt].virtue = virtue;
            p[cnt].talent = talent;
            p[cnt].sum = (virtue + talent);
            if (virtue >= H&& talent >= H)
            {
                p[cnt].type = 1;
            }
            else
            {
                if (virtue >= H)
                {
                    p[cnt].type = 2;
                }
                else
                {
                    if (virtue >= L && talent >= L)
                    {
                        if (virtue >= talent)
                        {
                            p[cnt].type = 3;
                        }
                        else
                        {
                            p[cnt].type = 4;
                        }
                    }
                    else
                        p[cnt].type = 5;
                }
            }
            cnt++;
        }
    }
    sort(p, p+cnt,cmp);
    cout << cnt << endl;
    for (int i = 0; i < cnt; ++i)
    {
        cout << p[i].id << ' ' << p[i].virtue << ' ' << p[i].talent << endl;
    }
    return 0;
}

 

posted @ 2020-09-30 10:10  幻想Elapse  阅读(103)  评论(0编辑  收藏  举报