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;
}
相关推荐
CCF_NOI.11 分钟前
洛谷P12610 ——[CCC 2025 Junior] Donut Shop
算法
我漫长的孤独流浪1 小时前
C++ 11
开发语言·c++
鑫鑫向栄1 小时前
[蓝桥杯]模型染色
数据结构·c++·算法·职场和发展·蓝桥杯
DoWeixin62 小时前
【请关注】VC内存泄露的排除及处理
c++·mfc·vc++·vc
1白天的黑夜13 小时前
栈-20.有效的括号-力扣(LeetCode)
c++·算法·leetcode
小白程序员丶钟同学3 小时前
L1-019 谁先倒 (15 分)
数据结构·算法
whoarethenext3 小时前
OpenCV C/C++ 视频播放器 (支持调速和进度控制)
c语言·c++·opencv
<但凡.3 小时前
题海拾贝:P2347 [NOIP 1996 提高组] 砝码称重
数据结构·c++·算法
bubiyoushang8884 小时前
matlab实现高斯烟羽模型算法
开发语言·算法·matlab