201503-2 数字排序

实现

#include <cstdio>
#include <algorithm>

#define MAXN 0x3ef

class figure_cnt
{
public:
    int figure;
    int cnt;

    figure_cnt() {};
    figure_cnt(int figure, int cnt): figure(figure), cnt(cnt) {};

    bool operator< (const figure_cnt& right) {
        if ((this->cnt > right.cnt)
        ||(this->cnt == right.cnt && this->figure < right.figure)) {
            return true;
        } else {
            return false;
        }
    }
};

int num_cnts[MAXN];
figure_cnt figures[MAXN];
int figures_cnt;

int main() {
    int num;

    scanf("%d",&num);

    for (int i = 0;i < num;++i) {
        int figure;
        scanf("%d",&figure);
        ++num_cnts[figure];
    }

    for (int i = 0;i < MAXN;++i) {
        if (num_cnts[i] > 0) {
            figures[figures_cnt++] = figure_cnt(i,num_cnts[i]);
        }
    }
    
    std::sort(figures,figures+figures_cnt);
    for (int i = 0;i < figures_cnt;++i) {
        printf("%d %d\n",figures[i].figure,figures[i].cnt);
    }
}
posted @ 2020-08-28 10:49  amonqsq  阅读(64)  评论(0编辑  收藏  举报