CCF CSP题解:数字排序(201503-2)

链接和思路

OJ链接:传送门

本文需求来自LA姐。本题仅需用一个辅助数据结构Tmp记录每1个数字的值(value)及其出现的次数(cnt),然后重载运算符<并针对cnt排序输出即可。辅助数据结构Tmp定义如下:

cpp 复制代码
struct Tmp {
    int value;
    int cnt;

    bool operator<(const Tmp &t) const {
        if (this->cnt != t.cnt)
            return this->cnt < t.cnt;
        return this->value > t.value;
    }
} result[1005];

其余见AC代码。

AC代码

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;

int a[1005] = {0};
int n;

struct Tmp {
    int value;
    int cnt;

    bool operator<(const Tmp &t) const {
        if (this->cnt != t.cnt)
            return this->cnt < t.cnt;
        return this->value > t.value;
    }
} result[1005];

int main() {
    cin >> n;

    for (int i = 0; i < n; i++) {
        int idx;
        cin >> idx;
        a[idx]++;
    }

    int curIdx = 0;
    for (int i = 0; i <= 1000; i++) {
        if (a[i] != 0) {
            result[curIdx].value = i;
            result[curIdx].cnt = a[i];
            curIdx++;
        }
    }

    sort(result, result + curIdx);

    for (int i = curIdx - 1; i >= 0; --i) {
        cout << result[i].value << " " << result[i].cnt << endl;
    }

    return 0;
}
相关推荐
a_j58几秒前
算法与数据结构(回文数)
数据结构
猎人everest2 分钟前
机器学习之正则化
人工智能·算法·机器学习
呵呵哒( ̄▽ ̄)"4 分钟前
绪论数据结构基本概念(刷题笔记)
数据结构
YaoSolar5 分钟前
刷题记录(LeetCode 78 子集)
算法·leetcode
熊峰峰14 分钟前
数据结构第八节:红黑树(初阶)
开发语言·数据结构·c++·算法
梅茜Mercy23 分钟前
C++:入门详解(关于C与C++基本差别)
java·c语言·c++
f狐0狸x1 小时前
【蓝桥杯每日一题】3.8
数据结构·c++·算法·蓝桥杯
牵牛老人1 小时前
C++设计模式-工厂模式:从原理、适用场景、使用方法,常见问题和解决方案深度解析
开发语言·c++·设计模式
阿巴~阿巴~1 小时前
动态规划填表技巧:固定最后一个数 vs 固定倒数第二个数
c++·算法·动态规划
我感觉。1 小时前
【机器学习chp12】半监督学习(自我训练+协同训练多视角学习+生成模型+半监督SVM+基于图的半监督算法+半监督聚类)
人工智能·算法·机器学习·半监督学习